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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

ETS User

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

Post 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");    
    } 
} 

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

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

Post 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!

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

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

Post by nickeasyuptech »

Thanks Paul, I really appreciate your help!

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

No idea what Eclipse is doing.
If you like our products and services, please help us by posting your review here.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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{ ... }
If you like our products and services, please help us by posting your review here.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post by Kevin_C »

Ah ok, thanks for correcting that. :)

alkour
Posts: 31
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

alkour
Posts: 31
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post 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.

ale8989
Posts: 5
Joined: Fri Nov 17, 2017 9:12 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ale8989
Posts: 5
Joined: Fri Nov 17, 2017 9:12 am
Contact:

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

Post 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.

gaborj
Posts: 3
Joined: Sat Mar 16, 2019 6:28 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

gaborj
Posts: 3
Joined: Sat Mar 16, 2019 6:28 am
Contact:

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

Post by gaborj »

I see, thanks a mil for the explanation.
Was a bit ambiguous for me but now I get it.

morarumaria1988
Posts: 5
Joined: Mon Oct 30, 2017 5:17 am
Contact:

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

Post by morarumaria1988 »

Hello,

Why the class main's main method is executed and not the class anothermain's main method?
Attachments
Skärmklipp.PNG
Skärmklipp.PNG (6.4 KiB) Viewed 1576 times

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

How exactly are you trying to execute? IOW, what exact command are you using to compile and execute?
If you like our products and services, please help us by posting your review here.

morarumaria1988
Posts: 5
Joined: Mon Oct 30, 2017 5:17 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 8852
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests