wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in statics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in statics" Watch "Help in statics" New topic
Author

Help in statics

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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...


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

can not override static method/variables . but can be accessed by sub class
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jul 15, 2009
Posts: 537

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.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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?


SCJP6
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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......
Abhijeet Pathak
Ranch Hand

Joined: Jul 05, 2009
Posts: 33
Dude it's a rule "You cannot access protected variables from subclass by creating an object of base class in the subclass."


I am the one who knows that I don't know anything.
Abhijeet
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jul 05, 2009
Posts: 33
Of course Jason you are absolutely right.
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
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

Joined: Jun 09, 2009
Posts: 327
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

Joined: Feb 16, 2009
Posts: 83
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

Joined: Jun 09, 2009
Posts: 327
Cool - just didn't want anyone picking up a wrong idea from the use of the word "override".
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jul 05, 2009
Posts: 33
Jason. Man you are seriously annoying me.Instead of simplifying the answer you confusing me as well as Nitish.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jul 05, 2009
Posts: 33
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

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jun 09, 2009
Posts: 327
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!
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
just when you think are thorough with a concept , along comes a doubt to shake you up
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Abhijeet Pathak wrote:Explain yourself.

Can you follow why this works?
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257


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


Experience and talent are independent of age
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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

Joined: Jul 05, 2009
Posts: 33
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

Joined: Jul 05, 2009
Posts: 257
yes! now things are better. It must be explicitly stated if the classes are in same or different packages.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in statics
 
Similar Threads
question about static inner class?
static method query [KB book (Topic 4.10).]
regarding inner classes
Question about static class
can we access a static variable in a non-static method?