• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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:


 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic