• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Elementary Stuff

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To all you javaGurus:
Why doesn't this work ?
class A {
int x ;

}
class A1 extends A {

public static void main(String[] argv) {

System.out.println(super.x) ;

}
}

While this does :
class A {
int x ;

}

class A1 extends A {

int y = super.x;
public static void main(String[] argv) {

A1 a1 = new A1();
System.out.println(a1.y) ;
}


}

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The keyword super in Java when used in the scope of a method is to call the overridden method from the overriding method. It is not meant to be used to access a superclasses variables. Therefore your use of super.x to try and grab a superclass value will not work. Since you are the subclass the correct way to perform it is:
System.out.println(""+x);
The 2nd usage works because the super keyword used in a class scope can access shadowed class variables. In your case, you have not created a 'x' variable in your subclass so you would get the same results if you left off the 'super.' all together:
int y = x;
Your syntax becomes interesting when you actually shadow the superclass variable:

The above code yields the result '100'!
Every little bit helps,
Manfred.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

x is an instance variable. It requires an Object of A to exist before it can exist. In this example, you haven't created any Objects, your just using the static method.

When you create an Object of a class, java creates all the super classes FIRST so when you do A1 a1 = new A1();
Java creates an object of Object (the super class to all classes and of A) Then it creates an Object of A which causes x to be intialized on the heap. Finally it creates an Object of A1 which causes y to be intialized to the value of A's x which now exists.
Hope this helps

[This message has been edited by Carl Trusiak (edited January 16, 2001).]
[This message has been edited by Carl Trusiak (edited January 16, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want you first example to work, you could make the variable static and set it to some printable value. Then remove the reference to super.

 
hari pillai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Trusiak:
[B]

x is an instance variable. It requires an Object of A to exist before it can exist. In this example, you haven't created any Objects, your just using the static method.



When you create an Object of a class, java creates all the super classes FIRST so when you do A1 a1 = new A1();
Java creates an object of Object (the super class to all classes and of A) Then it creates an Object of A which causes x to be intialized on the heap. Finally it creates an Object of A1 which causes y to be intialized to the value of A's x which now exists.

Hope this helps
[This message has been edited by Carl Trusiak (edited January 16, 2001).]

[This message has been edited by Carl Trusiak (edited January 16, 2001).][/B]



I do not think x being non static is the problem because even the following will not work:
class A
{
static int x = 100 ;
}

class A1 extends A
{
public static void main(String[] argv)
{
System.out.println(super.x) ;
}
}
I believe Leonhardt is right when he pointed out that "super" keyword is special in that it is to be used only from an overriding method for referring to an overridden method . However it can be used in Class scope of a subclass for referring to a variable of the superclass.

 
Hey cool! They got a blimp! But I have a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic