Page 1 of 2

About Question enthuware.ocajp.i.v7.2.845 :

Posted: Sun Dec 09, 2012 4:22 am
by ETS User
class main must be public to run main method with java main, it's correct? If yes, public is missed.

Code: Select all

//in file main.java
class anothermain{
    public static void main(String[] args) {
        System.out.println("hello2");
    } 
}

class main {    
    public final static void main(String[] args) {
        System.out.println("hello");    
    } 
} 

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Sun Dec 09, 2012 7:51 am
by admin
The main method has to be public. There is no such condition on the class. Even JLS is filled with examples with a public main method contained within a non public class.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Jun 18, 2013 1:50 pm
by nickeasyuptech
For option #3, you state

"A public class must exist in a file by the same name. So this code is invalid because anotherone is a public class but the name of the file is main. It would have been valid if the name of the file were anotherone.java."

Is that really the case? A public class must exist in a file by the same name? Or a public class does not need to exist but if it does, it has to be of the same name as the file?

It seems that a public static main method must exist but not a public class.

Thanks for your help!

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Jun 18, 2013 3:25 pm
by admin
That is an interesting (and valid) interpretation. You are right, a public class need not exist at all in a file but if exists, its name must be same as the name of the file.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Jun 18, 2013 8:51 pm
by nickeasyuptech
Thanks Paul, I really appreciate your help!

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Mon May 12, 2014 2:39 pm
by JeramieH

Code: Select all

Which of these options can be run with the following command line once compiled? 
java main

//in file main.java    
public static void main4(String[] args) ...

You cannot have a method on its own. It must be a part of a class.
I didn't even catch that it wasn't part of a class... I keyed in on the fact it's spelled "main4". Not sure if that was an intentional misspelling or a legit typo, since it's not addressed by the explanation.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Mon May 12, 2014 8:14 pm
by admin
It should have been just main instead of main4 for the option to be of some strength. It is wrong either way though.
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Oct 07, 2014 4:55 pm
by Daniel Clinton
In the 4th option (the correct one):

Is it not possible that java main
from the command line could, in theory, cause anothermain's
main method to execute?
Certainly that configuration was simple to achieve
using Eclipse's Run Configuration.

EDIT-
Oh my mistake, I think I've worked it out.
I've realised that the two classes although in the one file
are compiled into two bytecode files.
And so I guess all Eclipse is doing is running a script to invoke
java anothermain whenever I run main.java.
Would that be about right?

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Oct 07, 2014 7:40 pm
by admin
No idea what Eclipse is doing.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Nov 11, 2014 8:51 am
by Kevin_C
From the explanation of the last two answer, is the following correct:

- A file must contain a class with the same name
- Only the class with the same name as the file can be public (but this is optional)

So:
This is valid:

Code: Select all

// in file main.java
class main{ ... }
class anothermain{ ... }
This is valid:

Code: Select all

// in file main.java
public class main{ ... }
class anothermain{ ... }
This is invalid:

Code: Select all

// in file main.java
public class anothermain{ ... }
This is invalid:

Code: Select all

// in file main.java
class main{ ... }
public class anothermain{ ... }
This is invalid:

Code: Select all

// in file main.java
public class main{ ... }
public class anothermain{ ... }
The only one I'm doubting about is the last one, with multiple public classes in one file

I've tried all of them myself and it is indeed correct what I stated above. I'll just leave this here for anyone else doubting about what is valid and invalid.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Nov 11, 2014 8:35 pm
by admin
- A file must contain a class with the same name
This is not true. A file may have a non-public class with a different name. So the following is valid:

Code: Select all

// in file main.java
class TestClass{ ... }

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Wed Nov 12, 2014 3:20 am
by Kevin_C
Ah ok, thanks for correcting that. :)

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Wed Apr 08, 2015 4:20 am
by alkour
Hi,


Could I ask to check whether I understood correctly logic:
1. In the file, it should be the class with the same name as the file name. With public modifier or not.
2. Within this class should be the main method with the signature:
- public static void main(String[] args)
- Or public static final void main(String[] agrs).

To confirm the rule in D option I commented class main and left class another. The code did not compile because compiler could not find main method.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Wed Apr 08, 2015 4:44 am
by admin
alkour wrote:Hi,
Could I ask to check whether I understood correctly logic:
1. In the file, it should be the class with the same name as the file name. With public modifier or not.
No. A file can have a class with any name if the class is not public.
2. Within this class should be the main method with the signature:
- public static void main(String[] args)
- Or public static final void main(String[] agrs).
No, a class need not necessarily have main method.
To confirm the rule in D option I commented class main and left class another. The code did not compile because compiler could not find main method.
Compiler doesn't care about main method. The JVM does, if you try to run the class.

I would like to suggest you to go through a book or a tutorial to get these basics first before attempting mock exams.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Wed Apr 08, 2015 8:11 am
by alkour
I would like to suggest you to go through a book or a tutorial to get these basics first before attempting mock exams.
HTH,
Paul.
Actually I have already did three tutorails, including Oracle one.

But I found your questions as the best tool to validate and sharp the knowledge about Java.
Thank you for this.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Mon Dec 18, 2017 11:34 am
by ale8989
Hello everyone,
I have another question, given that:
° file name could be anything if there is no public class defined in such file
° file name has to be the same of the public class IF there is one
° could be more than one class with a main method in same file

I tried to run a file with 3 classes and within 2 of them was a main method.
File name is completely different from the 3 cointained classes.

Compiles fine, JVM runs fine, but...
How can the JVM know which class to run if there are 2 main methods present?

I tried also to change the appereance order of the classes, but JVM run always the same class.


Code: Select all

class A{
     final int fi = 10;
}

class C {

   public static void main(String[] args) {

      System.out.println("inside C class");
      
   }
}

 class B extends A{
     int fi = 15;    
     public static void main(String[] args){        
      B b = new B();        
      b.fi = 20;        
      System.out.println(b.fi);        
      System.out.println(((A)b).fi);
      } 
}
It prints always the statements in class B.
File's name is : Base.java

Why?

Any help would be greately appreciated.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Mon Dec 18, 2017 11:26 pm
by admin
Well, your confusion arises from your misunderstanding that you are running a "java file".

You need to understand that you don't run a Java file. You run a class file, which is produced after compiling the java file. Now, after compiling this java source file of yours, how many class files do you see?

While running a class from command line, what class do you specify? Whichever class you specify, the JVM will pick that class's main method to execute. This has nothing to do with the source code file.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Dec 19, 2017 4:15 am
by ale8989
Thank You Paul,
I've understood!

For clarification:

Yesterday JVM kept compiling the same Base .java file(from a previous test),in which there was a java class called "Base" with its main method and JVM would execute that .class file. (my mistake)

Today I've deleted all the previous .class file and compiled the example in my last post.
When I tried to run it -> JVM : "Could not find or load main class Base", that's because there is no class named like that in my source code.

Thank you for your help!
p.s. there are as many .class files as there are java classes inside a .java file that will be compiled.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Thu Mar 21, 2019 2:55 pm
by gaborj
Hi, could you please elaborate this:
'making a static method final prevents the subclass from implementing the same static method.'

I get that final prevents the implementation but how would it be possible to implement the same static method anyway?
I thought static methods are hidden if subclass has method with the same name.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Thu Mar 21, 2019 8:07 pm
by admin
If the static method is not final, the subclass can also have a static method with the same signature ie it can hide the super class's method.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Fri Mar 22, 2019 7:57 am
by gaborj
I see, thanks a mil for the explanation.
Was a bit ambiguous for me but now I get it.

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Sat Dec 21, 2019 12:00 pm
by morarumaria1988
Hello,

Why the class main's main method is executed and not the class anothermain's main method?

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Sat Dec 21, 2019 12:18 pm
by admin
How exactly are you trying to execute? IOW, what exact command are you using to compile and execute?

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Tue Dec 24, 2019 11:54 am
by morarumaria1988
Actually I didn't try to execute it via command line by myself as I should, but after rereading previous comments from you and ale8989 I understand that when executing it via cmd line the JVM is looking for a class that has the same name as the java file and that contains a valid main method. And if there is none, then JVM returns: "Could not find or load main class X" (where X is the name of the file)
So that's why in our case the main method of main class is executed, because the file we compiled is called "main", the same as the class "main". Did I understood it right?

Re: About Question enthuware.ocajp.i.v7.2.845 :

Posted: Wed Dec 25, 2019 3:23 am
by admin
No, you specify the name of a class to the java command. Java then looks for a java class by that name in its classpath and if found, it then looks for the main method in that class.
Please go through this topic from a good book to make sure you understand the difference between java file and class file.