aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class using Main without declaring itself Public Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class using Main without declaring itself Public " Watch "Class using Main without declaring itself Public " New topic
Author

Class using Main without declaring itself Public

Mukesh Sheoran
Greenhorn

Joined: Nov 24, 2008
Posts: 12
This is question no 11 in chapter 2 in K S book for SCJP 1.5

Given:
class Uber {
static int y = 2;
Uber(int x){this(); y= y* 2;}
Uber(){y++;}
}
class Minor extends Uber{
Minor (){ super(y); y = y+3;}
public static void main(String[] args) {
new Minor();
System.out.println(y);
}

}
What is the result?
a. 6
b. 7
c. 8
d. 9
e. Compilation fails.
f. An exception is thrown

Answer Given is "d"
But According to me it should be " e "
Reason:
1. Main method should be in a Public class but class Minor is not declared as public .

Please clarify
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
There should be only one public class per source file.
But it is not compulsory that the class which contains the main method should be public.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


Here Ulber class is public. Check whether now its compiling.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
yes, it is compiling and running Abhi.



SCJP 6
Mukesh Sheoran
Greenhorn

Joined: Nov 24, 2008
Posts: 12
Thats OPk, But I think the class that contains Mian method should be Public . Is it so???
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
class Uber is public, should be declared in a file named Uber.java
public class Uber {

I am getting a Compiler Error.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
I am compiling Minor.java.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Abhi vijay wrote:class Uber is public, should be declared in a file named Uber.java
public class Uber {

I am getting a Compiler Error.


Just look closely at punit's post...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Yes, he is compiling Ulber.java
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Mukesh Sheoran wrote:Thats OPk, But I think the class that contains Mian method should be Public . Is it so???



Not necessary.
Abhishek Sangole
Greenhorn

Joined: Jan 26, 2009
Posts: 16
Friends i have compiled and run these tow classes without keyword public.
The code works fine. I didnt get any compilation error in both classes.


~Abhishek~
passed SCJP1.5
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Yes, there wont be any compilation error.

If you have both these classes in one source file say, Minor.java(which contains the main method). And you mark the other class Ulber.java as public then you will get compiler error.
I hope I am not confusing you.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Yes, there wont be any compilation error.

If you have both these classes in one source file say, Minor.java(which contains the main method). And you mark the other class Ulber.java as public then you will get compiler error.
I hope I am not confusing you.


You are confused here Abhi.



this is the code and no compiler error here, although minor.java has main and ulber.java is declared public.

this is how to run:
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yes, you are right. But Abhi specified that both classes were in Minor.java. (I think he meant Ulber as opposed to Ulber.java later in his post.) If you have a public class in a source file (and you can only have one public class, interface, or enum in a source file) then the name of the source file must be the name of the public class, interface, or enum. That's enough for compilation. For running, you have to provide the name of the class that contains the main() method like you already said.


All code in my posts, unless a source is explicitly mentioned, is my own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class using Main without declaring itself Public
 
Similar Threads
Static Confusing
increasing and decreasing in loop
Doubt in Constructor
Static Doubt
question about static members