• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

accessability about protected instances

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

public class Pack {
static public int x1 = 7;
protected int x2 = 8;//#1
static int x3 = 9;
static private int x4 = 10;
}

import pack.Pack;

public class Test extends Pack {
Test(){
try{
throw new RuntimeException();
}finally{
System.out.println("runtime");
}
}
public static void main(String[] args){
Pack p = new Pack();
System.out.println(p.x2);
}
}

why #1 x2 can be accessed?
if you delete "static" modifier,then x2 is invisible?
can someone explain why?
thank you in advance^^
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When the subclass is in another package, it can only see a protected member through inheritance. You can't use a reference to access it.
 
Storm Zcm
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reply.I know than you can't use a reference of the protected instance if they are not in the same package.
What i means is that if you place "static" modifier before "protected",you can use a reference of the protected instance.
What's the reason?
reply
    Bookmark Topic Watch Topic
  • New Topic