Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problem related modifier "protected"

 
Ranch Hand
Posts: 33
MyEclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}

________________________________________________

import packed.pack;
class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 );
}
}


this is giving output "8". But if i removes static modifier from x2, there is an error. Please explain
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously there will be an error. When you remove "static" x2 becomes an instance variable (can be accessed only with object) and you cannot access it with class name.
It has nothing to do with "protected" access modifier.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you have 2 ways how to access x2 here; either keep static keyword and you can access it directly because you have copy of x2 inherited in your testpack class anyways. Or you can remove the static keyword then use an instance of testpack class to access the inherited copy of x2, check below for sample:

Either:
in pack:
in testpack:

OR:
in pack:
in testpack:



You can also use some getter method if you want but I don't think this is the main question here.

Cheers!!!
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Palash, since x2 is protected, so class test can only access it through inheritance. So if you make it non-static, and try to access it through an instance of pack class, then you'll get an error





Also when you post a source code, use code tags to make the code readable...
 
Palash Kumar
Ranch Hand
Posts: 33
MyEclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
 
I brought this back from the farm where they grow the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic