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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Markus, exam 1, Q43  RSS feed

 
Greenhorn
Posts: 8
  • 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
  • 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!