• 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

Help in statics

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static members are one for the class.. but are they inherited when an other extends a class with static members???please help me on this issue...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can not override static method/variables . but can be accessed by sub class
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Means the static variables are inherited.....hmmm the protected and public static variables will also be inherited by the subclass?

class A{ protected static int x =1;} this is in different package

different package;
class B extends A
{
A a = new A();
B b = new B();
System.out.println(a.x+" "+b.x);
}

what is the logic behind this program execution?I know a should not access x as it is protected n can only be accessed by b.But this works fine without error. Why?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood one point that protected members are inherited and can be accessed directly without the reference variable.

The thing i don't understand is y does "a.x" work......I know x could have been accessed by A.x. But why does the reference variable work.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags

You cannot override statics this is true. They are resolved at compile time based on the reference type. You can, however, redefine them. This is not overriding and a bit beyond the discussion just now. Best thing to do is experiment.

B extends A. This means that B gains all public/protected members of A (not "default" members as B is in a different package and never "private" members).
This means B has access to x.

So in your code b.x or B.x would be fine, although it is more correct to access a static member from the class e.g. A.x. Why? Because there is no explicit need to create an instance to access a static member and it can thus be more efficient if you get into that habit.

Why do a.x and A.X work? Well a IS-A A (an instance of A), so why wouldn't it work?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing that i am confused about is the access modifier>----> protected. As the things marked protected can only be accessed by inheritance.

package something;
class A
{
protected int i = 1;
}

package somethingdifferent;
class B extends A
{
public static void main(String... args)
{
A a = new A();
B b = new B();
System.out.println(a.x);//error this doesn't based on the definition of protected even if a IS-A A;
System.out.println(b.x);//this works n i know why it does
}
}

i meant to ask why does a.x works when x is marked static protected......
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude it's a rule "You cannot access protected variables from subclass by creating an object of base class in the subclass."
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijeet Pathak wrote:Dude it's a rule "You cannot access protected variables from subclass by creating an object of base class in the subclass."


Of course you can - so long as you are in the same package or if in a different package, working within a sub-class.

a.x works because the class that is being use (B) is a subclass of A and thus has visibility of x. If you tried that from C (which does not extend A) you'd have quite a different answer (assuming that C was in a different package to A, of course)
 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course Jason you are absolutely right.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

can not override static method/variables . but can be accessed by sub class



Sorry, the statement above is correct only for methods and NOT for variables because you can inherit a static variable from the parent class and you can override it by changing its value. Give it a try to see.

Cheers!!!
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mo Jay wrote:you can inherit a static variable from the parent class and you can override it by changing its value. Give it a try to see.


That's not strictly correct.

You cannot override a static in a sub, merely redefine it. This is because statics are processed and linked at compile time and thus depend on the reference type.
A sub class can, of course, still change the value contained in the static.
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A sub class can, of course, still change the value contained in the static.



This is what I am talking about, you can still access a static variable from subclass just by typing the variables name. You can also directly assigning that static variable from the parent a new value in the subclass and it works just fine. This is what I am referring to, not to go through some methods to do the modification.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool - just didn't want anyone picking up a wrong idea from the use of the word "override".
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys don't get me wrong.....Your answers are correct but its not the answer for my question.

Abhijeet you are right that protected variables cannot be accesed from a different class.

my question is when the variable in class A was declares as protected static. it was accessed using the class A's reference variable a.x. It worked properly. I am asking why does it work.

When the variable is just protected then yes abhijeet's answer is right but when it is declared as protected static then it can be accessed.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:Abhijeet you are right that protected variables cannot be accesed from a different class.


Only if it is not a subclass or in a different package.

my question is when the variable in class A was declares as protected static. it was accessed using the class A's reference variable a.x. It worked properly. I am asking why does it work


Because the class doing the accessing is a sub-class of A. See above.

When the variable is just protected then yes abhijeet's answer is right


Not always. It will still work if subclasses are in the same package.

but when it is declared as protected static then it can be accessed.


static has the effect of allowing any subclass, even ones in another package, to access it; as it is no longer tied to the instance.

Think about the equals method for a secondSo...why can I access "importantInt" on another class when it is private? Because I am accessing it from an instance of the same class. The exact same logic holds true for "default" and "protected"; subclasses have access to the base class members as well (but not if the subclass is in a different package in the case of "default").
 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason. Man you are seriously annoying me.Instead of simplifying the answer you confusing me as well as Nitish.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a really, really basic concept; absolutely fundamental to the language. If you are having trouble understanding then I rather doubt the problem lies with me.

Can a sub-class access a protected member on an instance of the super-class? Yes.
Does static have anything to do with it? No.
 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish wrote:When the variable is just protected then yes abhijeet's answer is right



Jason wrote: No it is not. See above.



Explain yourself.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason its quiet clear now.... Thanks and don't be angry ok... This seems to be a simple topic of statics but it had stuck on my mind that why it does work this way and not that way. Well now its clear Thanks for the reply.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:Jason its quiet clear now.... Thanks and don't be angry ok... This seems to be a simple topic of statics but it had stuck on my mind that why it does work this way and not that way. Well now its clear Thanks for the reply.


Cool. That sometimes happens. I'm not angry - it's just be a very long day.

Trust me, you'll know when I'm angry!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just when you think are thorough with a concept , along comes a doubt to shake you up
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijeet Pathak wrote:Explain yourself.


Can you follow why this works?
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This wont compile if "x" is not static in A. Hence static has a role to play.
And I clearly assume A and B are in 2 different packages.

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

Himanshu Kansal wrote:[code]This wont compile if "x" is not static in A. Hence static has a role to play.
And I clearly assume A and B are in 2 different packages.


Yes, if x is not static then a.x would fail if they are in different packages.

My sweeping statement was a bit too sweeping - I'd best update it in case it confuses others
 
Abhijeet Pathak
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:

Abhijeet Pathak wrote:Explain yourself.


Can you follow why this works?


Yeah.It will work fine as both super and sub are in same package.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes! now things are better. It must be explicitly stated if the classes are in same or different packages.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one line from Jason is enough to explain the whole problem. Thanks a lot jason
static has the effect of allowing any subclass, even ones in another package, to access it; as it is no longer tied to the instance.

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

Nitish Bangera wrote:Static members are one for the class.. but are they inherited when an other extends a class with static members???please help me on this issue...



Static members are not inherited, they are Class level not Instance level
reply
    Bookmark Topic Watch Topic
  • New Topic