• 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

Accessing static methods and variables

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across the following code in SCJP6 by kathy and bert,chpt:Object orientation,pg:151,the output is given as: a a a
but when i tried it out it had errors.

class Animal1
{
static void doStuff()
{
System.out.println("a ");
}
class Dog extends Animal1
{
static void doStuff()
{
System.out.println("d ");
}
public static void main(String args[])
{
Animal1 [] a={new Animal1(),new Dog(),new Animal1()};
for(int x=0;x<a.length;x++)
a[x].doStuff();
}
}
}

Please compile and check it out.I got errors on line no:9,13 and 15.Sorry but the attachment was taking a lot of time to load

Thankyou.

 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a problem with your code structure.

Your code with decent spacing:


Dog acts as an inner class of Animal1, and inner classes cannot have static declarations. You should treat them both as separate classes, closing off Animal before starting Dog.


This ought to work.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic