• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regarding static classes

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any one explain me the following code...


In the following case ans is "B"

11. static class A {
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. new B().process();
19. }
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose A and B are static nested classes.
Class B overrides process() method : it's ok, it doesn't have to throw Exception.
From main method, there's a call to process() method on an instance of class B.
The output is "B"
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But when Itried to compile the code i got errors
C:\Documents and Settings\10085\Desktop\Practise>javac B1.java
B1.java:1: modifier static not allowed here
static class A1 {
^
B1.java:4: modifier static not allowed here
public static class B1 extends A1 {
^
2 errors

Here classes A is cahnged to A1 and B is B1
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class can only be marked static if it's a static nested class.
Example:
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siva,

Do quote your sources...

Normally you can't use the static modifier to a class..
for eg:

is wrong... Thats meaningless too.


but



is okay.. Now AreaInfo is a class thats nested in House and its a static member of House class. That means that you can create an instance of AreaInfo even without having an instance of House.

In the question, the line no starts from 11... so we assume that theres a class in which two classes A and B are nested.. and hence the output...

Regards,
Vishwa
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot....
Now I got the point
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic