• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

can class be static?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have this question

static class A {
void process() throws Exception { throw new Exception(); }
}
static class B extends A {
void process() { System.out.println("B"); }
}
public static void main(String[] args) {
new B().process();
}

Options :
A. B
B. runs but no output
rest of the options are Compilations Errors

Answer is A
i have no problem with the answer
but this code raised a few questions for me
can classes be static??? according to the code yes but when and under what conditions???
and how am i supposed to use new with something that is static???
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like there's something missing from the question.

A normal class can't be static. What would that mean? Something that is static belongs to the class, not an instance, but that distinction is irrelevant for a normal class.

A nested class (or inner class) is different. They can be static. But in that case the classes would be defined within another class.

I think the giveaway here is the main method. That also needs to belong to a class, but in the code given it doesn't. So either compilation would completely fail, or all this is defined within a class that is not shown.
 
Greenhorn
Posts: 14
Oracle Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Maxwell It seems that you have not taken out the whole questions
well
iff it is whole then it would not compile
inner classes can be static for further study refer chapter 8 from k&b
regards
 
Maxwell Wood
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah ... something is not right with the question... but i found it in a few sources exactly the way i wrote it and with the same output
it's also on this site in another thread https://coderanch.com/t/267905/java-programmer-SCJP/certification/four-scenarios
i have even tried it on NetBeans and it would NOT compile

so far i have realized this : i can have static class which is an inner class
but my problem is with the idea of using static member + new
would someone please explain that point?
 
Ranch Hand
Posts: 75
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If class B is not static:


 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic