• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Class using Main without declaring itself Public

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here Ulber class is public. Check whether now its compiling.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, it is compiling and running Abhi.

 
Mukesh Sheoran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am compiling Minor.java.
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, he is compiling Ulber.java
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic