• 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

Instance variable access

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have the following code:



A compilation error occurs. This is due to the assignment statement in the Subclass class. Is this due to the fact that instance variables and methods are not available until the superclass constructor completes? I just want to make sure that my understanding is correct.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: what exactly is this code trying to do?




And what does the compiler error tell you?
[ January 23, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error message isn't very clear, but think about where in a class you can put an assignment statement.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U cannot put the assignment there, use it inside a method
like:
public class SuperClass {
protected int i = 10;
}
class Subclass extends Superclass {

public void method() {
i = 10;
System.out.println("i is " + i);
}
}

This would compile successfully
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I am not clear in this ....
what wrong in this ... we are reinitialising a variable ....
thanks .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But u cant initailise that just anywhere, it should be done within a block
u can only declare items outside.
Like u can have also this:

public class SuperClass {
protected int i = 10;
}
class Subclass extends Superclass {
{
i = 15;
}
public void method() {
System.out.println("i is " + i);
}
}
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So back to my question. Is the compilation failure due to the fact that instance variables are not available until the super constructor runs? I found this rule in the K & B book, but not sure if this is exactly what the rule meant.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Liang Anmian:
So back to my question. Is the compilation failure due to the fact that instance variables are not available until the super constructor runs? I found this rule in the K & B book, but not sure if this is exactly what the rule meant.




As mentioned above, your code is syntatically not correct Java.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
subclasses do not inherit variables defined in the superclass.
So variable i in subclass needs to be assigned a type to compile it correctly.

Thanks
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

subclasses do not inherit variables defined in the superclass.



That is wrong! Isn't it?
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Vagner:


That is wrong! Isn't it?



Yup it's wrong. In my example, the instance variable defined in the superclass is protected. Since it's protected, the subclass has access to the variable.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic