• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class using Main without declaring itself Public

 
Mukesh Sheoran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here Ulber class is public. Check whether now its compiling.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it is compiling and running Abhi.

 
Mukesh Sheoran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats OPk, But I think the class that contains Mian method should be Public . Is it so???
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am compiling Minor.java.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, he is compiling Ulber.java
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic