• 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

scope question

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ranchers.
someone can i help me ?
I do not understand why the next code compiles ?

if you delete the static modifier of the line1 does not compile, what is the rule ?


package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8; // line1
static int x3=9;
static private int x4 =10;
}


package finalscjptiger;
import packed.pack;

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

Thank in advance
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just change the main method,,, ReSort your Application...
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that the main method is always in a static context. If you remove the static modifier from the variable, it becomes an instance variable and can't be accessed in a static context.
 
Carlos G�mez
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wrote:

package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8; // line1
static int x3=9;
static private int x4 =10;
}


package finalscjptiger;
import packed.pack;

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

Im sorry ranchers, i found a mistake in the question ... you must change the next line:
System.out.println( p.x2 ); // line 2

If you change the line, i do not understand why the code compiles ?
if you delete the static modifier on the line1 does not compile, what is the reason ?
 
Ranch Hand
Posts: 133
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because x2 is protected so if it's not static it could be only accessed through sublcass test.

try this:

pack p = new test();
System.out.println( p.x2 );
[ November 22, 2006: Message edited by: Krzysztof Koziol ]
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if you delete the static modifier of the line1 does not compile, what is the rule ?


Rule 1: The protected member can be accessible to the classes which are declared in same package in which the class declaring the member exists or accessible to the sublclss of this class.
Rule 2: protected members can be accessed by their subclassed only through inheritence.
if the member is instance variable or instance methods they has to follow both the rules while for the members which can not be inherited like (static members and constructors) has to follow the rule 1 only.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic