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

About Static

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could some one explain me why this program is not getting compiled.

public static class ABC {

}

Can't we have any static class like this ?
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Polishetty Raghavendra:
...
Can't we have any static class like this ?



No we can not have static class like that.

The topmost classes can not be static, you can have a class inside another class and that inner class can be static.
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Top level class cannot be static. Only inner class or nested classes can be declared as static. this link will help you to understand how to use static keyword with classes.
 
Polishetty Raghavendra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in One of the MockExam Question is :

Given:
static classA {
void process() throws Exception { throw new Exception(); }
}
static class B extends A {
void process() { System.out.println(�B �); }
}
public static void main(String[] args) {
A a=new B();
a.process();
}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.

Answer: F

is this Answer correct in this Case ? If Yes How ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is line 19?
 
Polishetty Raghavendra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Complete Question with the Line numbers :

Given:
11. static classA {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(�B �); }
16. }
17. public static void main(String[] args) {
18.A a=new B();
19. a.process();
20.}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.
Answer: F
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polishetty, you must have omitted some of the code of that mock question. There must be a class SomeClass { ... } declaration around that code, so that classes A and B are static nested classes of the top-level class SomeClass.

Note that you also can't declare a method outside a class - the main() method declared in line 17 must be inside a class, otherwise it won't compile.

If there is a class declaration around the code, then the answer is indeed F. The error in that case is that you're not handling the exception thrown by the process() method. You must either handle the exception by surrounding the call with a try-catch block, or you must add 'throws Exception' to the declaration of the main method.
[ August 22, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Modified code based on what Jesper said:



The code does not compile on a.process(); since the compiler is expecting the process method os class A to be called and the exception thrown by the method has not been caught.
 
Polishetty Raghavendra
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I pasted actual question what was given.
That's the reason i tried to separate them, I declated a class as

static class ABC // it's giving a compile time error.

Anyhow, I came to know that we can't declare a class like this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic