Win a copy of Clojure in Action this week in the Clojure forum!
    Bookmark Topic Watch Topic
  • New Topic

Markus, exam 1, Q43

 
Jeff L.
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
What will be the result when you try to compile and run the following code?
private class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
Pri p = new Pri();
System.out.println(i);
}
}
1) Error at compile time
2) 200
3) 100 followed by 200
4) 100
---------------------->
The answer listed is 1), because the code doesn't compile, presumably assuming that the java filename is Base.java, but why assume that? Why couldn't the file be named Pri.java? I compiled a Java class with the public class listed second just this afternoon and it worked fine.
 
Jeff L.
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Oops! I should have said I tried earlier to compile where the public class was listed after another package scope class. Apparantly, package classes and private classes are treated differently. Private classes cause an error "modifyier private not allowed here", while package classes compile just fine. IMO, this aspect of Java is silly, but the question was correct about there being a compiler error. My bad.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic