• 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

protected access

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As we know when a class has a protected instance variable it can be accessed by the sub class in other package only with the subclass object. ie new SubClass().variable; But it only looks for the reference type and if the statement was something like this.

SuperClass object = new SubClass();
The protected variable cannot be accessed using object as the reference type is of SuperClass. However if i declare the protected variable in the superClass as static it can be accessed in the subclass using the superClass reference. Why does the behaviour of the protected keyword change when used with static
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There wasn't really a question there, but...

You can only access the protected variable within she scope of the subclass.

i.e. if this.variable is also valid,

you cannot use the subclass and have access to the protected variable of the superclass, it remains protected.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i have some detial explanation please. It would be nice if someone ellobaorate it a bit as if why the behaviour changes with the keyword static being added.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can See Error for both, check this Example ...As per my knowledge we can access the protected only throw inheritance relation ship (when we are out side the packages)...


------------
package com.passionsoft.test;

public class A {
protected static int count = 0;
protected int count_non_static = 0;
}

---------------
package com.passionsoft.test;

public class b extends A {

}
-----------------



package com.passion4java.mail;

import com.passionsoft.test.*;


public class SimpleExample {

public static void main(String[] args) {

A aref = new b();
b bref = new b();
System.out.println(" "+A.count); //Error
System.out.println(" "+aref.count_non_static); //Error
System.out.println(" "+bref.count); //Error
System.out.println(" "+b.count_non_static); //Error

}
}
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bit difficult to understand your question, but static variables behave differently from instance variables. You ought not to use super with static variables anyway. You ought to use ClassName.fieldName.

Read this FAQ about a related subject, and see whether that helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic