This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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?
 
Happily living in the valley of the dried frogs with a few tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic