• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Accessing "main" in package-private class

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a class is declared w/o a modifier, it can be described as package-private.



What is the result of attempting to compile and run the above program?
a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above


Can someone clarify what it means to "run" the program? It seems that the question should instead be "What happens when Q.main is called."
Really, there's no way to run this from the command line without any auxiliary code.
What is the wording like on the exam? Like this?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say that the result will be a runtime error, because of what you just said: it doesn't have a public modifier before the Q class declaration. So when you try to compile and run the code, the compiler will say something like: exception in thread "main" java.lang.NoClassDefFoundError...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not really an answer, but another question based on your code example.
When I ran Q's main I got
P.printS1
Q.printS2
I was not expecting this result - can someone explain?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Smith:
When a class is declared w/o a modifier, it can be described as package-private.

What is the result of attempting to compile and run the above program?
a. Prints: P.printS1 P.printS2
b. Prints: P.printS1 Q.printS2
c. Prints: Q.printS1 P.printS2
d. Prints: Q.printS1 Q.printS2
e. Runtime Exception
f. Compiler Error
g. None of the Above
<hr></blockquote>
Can someone clarify what it means to "run" the program? It seems that the question should instead be "What happens when Q.main is called."
Really, there's no way to run this from the command line without any auxiliary code.
What is the wording like on the exam? Like this?


Garrett,
When you declare a public class...that means you can only declare one and only one public class.
If you dont declare any class or interface public as in the above code, it is ok to name the file as any of the existing classes. The runtime will check whether there is a main() method in the class whose name is the filename. Otherwise , a runtime exception is thrown.
On the other hand, when you declare a public class, that should be the name of the file in which other classes can exist. and also the public class must have the main method with the required signature to run.
see this code below


IN the above code, if you name it as Hello1.java, then its main method is run at runtime. If you name it Hello2.java, its main method is run and so on. Play with it and let me know if thats what you wanted to know
Sri
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm ok on that now. I thought to run a program's main from the command line, the class had to be coderanch. I don't know why I thought this. I'm having a kind of stupid day today.
As for the other question regarding overriding and hiding, I've been reading §8.4.8.4 and 8.5.8.5 of the JLS.
Try this thread:
http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html
https://coderanch.com/t/370632/java/java/static-methods-do-not-participate
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garrett,
i thought only public class was the problem...Otherwise, I would have given an example of overriding and hiding(static methods are hidden not overridden)
And thats a very important point to note when you are appearing for the exam... because I did get a question testing those concepts
Sri
 
Monisha Talwar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Garrett,
Both the links u sent were awesome...I think I've understood the concept now.
http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html
https://coderanch.com/t/370632/java/java/static-methods-do-not-participate
--------------------
 
Sam Stoikol
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ups! I think I was wrong, sorry but this was my first time posting a message here. I'll be more careful next time, especially because I need to pass the test next week.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic