public static in package access class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selimsivad83
    New Member
    • Dec 2006
    • 4

    public static in package access class

    Hi everyone,

    Thinking in Java, 4th edition, p 232: ... if you don't put an access specifier for class access, it defaults to package access. ... However, if a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class.
    However the code below produces an error:
    -------------------------------------------------------------------------------------------------
    package com.dizzybird.t est;

    class Foo
    {
    public static int i = 1983;
    }

    -------------------------------------------------------------------------------------------------
    package com.dizzybird.t est.anothertest ;

    import static com.dizzybird.t est.Foo.*;
    // import com.dizzybird.t est.*;

    class Test
    {
    public static void main(String[] args)
    {
    System.out.prin tln(i);
    // System.out.prin tln(Foo.i);
    }
    }

    -------------------------------------------------------------------------------------------------
    Test.java:3: com.dizzybird.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package
    import static com.dizzybird.t est.Foo.*;
    ^
    Test.java:10: cannot find symbol
    symbol : variable i
    location: class com.dizzybird.t est.anothertest .Test
    System.out.prin tln(i);
    ^
    2 errors
    -------------------------------------------------------------------------------------------------
    if the commented code is used instead:

    Test.java:11: com.dizzybird.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package
    System.out.prin tln(Foo.i);
    ^
    1 error
    -------------------------------------------------------------------------------------------------
    Any ideas on that?
    Thanks for reading my question.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by selimsivad83
    Hi everyone,

    Thinking in Java, 4th edition, p 232: ... if you don't put an access specifier for class access, it defaults to package access. ... However, if a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class.
    However the code below produces an error:
    -------------------------------------------------------------------------------------------------
    package com.dizzybird.t est;

    class Foo
    {
    public static int i = 1983;
    }

    -------------------------------------------------------------------------------------------------
    package com.dizzybird.t est.anothertest ;

    import static com.dizzybird.t est.Foo.*;
    // import com.dizzybird.t est.*;

    class Test
    {
    public static void main(String[] args)
    {
    System.out.prin tln(i);
    // System.out.prin tln(Foo.i);
    }
    }

    -------------------------------------------------------------------------------------------------
    Test.java:3: com.dizzybird.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package
    import static com.dizzybird.t est.Foo.*;
    ^
    Test.java:10: cannot find symbol
    symbol : variable i
    location: class com.dizzybird.t est.anothertest .Test
    System.out.prin tln(i);
    ^
    2 errors
    -------------------------------------------------------------------------------------------------
    if the commented code is used instead:

    Test.java:11: com.dizzybird.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package
    System.out.prin tln(Foo.i);
    ^
    1 error
    -------------------------------------------------------------------------------------------------
    Any ideas on that?
    Thanks for reading my question.

    " the client programmer can still access that static member"
    You are trying to access the class not the member. Notice that the error is on the import statement for the class. Change it to

    Code:
    import static com.dizzybird.test.*;
    and you will see what they are talking about.

    Comment

    • selimsivad83
      New Member
      • Dec 2006
      • 4

      #3
      THANKS ALOT for bothering to read and answer my question :)
      Actually what you're saying doesn't seem correct.
      the static import expects a class or interface.
      for example if you write
      Code:
      import static com.dizzybird.test.*;
      the interpreter expects a class named test, of which the static members it will
      make available.
      if you don't want to use .*; you would write
      Code:
      import static com.dizzybird.test.Foo.i;
      if the class declaration is like this
      Code:
      class Foo
      {
          public static int i;
      }
      in the case i referred to, test is a package so what you suggested won't work.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by selimsivad83
        THANKS ALOT for bothering to read and answer my question :)
        Actually what you're saying doesn't seem correct.
        the static import expects a class or interface.
        for example if you write
        Code:
        import static com.dizzybird.test.*;
        the interpreter expects a class named test, of which the static members it will
        make available.
        if you don't want to use .*; you would write
        Code:
        import static com.dizzybird.test.Foo.i;
        if the class declaration is like this
        Code:
        class Foo
        {
        public static int i;
        }
        in the case i referred to, test is a package so what you suggested won't work.
        Alright I see my mistake. Your arguement is still wrong though. Remove the static import then(It is not what you are testing.). You tried to acces the protected class in a different package that gave the error. If you want to import the Foo class you cannot do it using static import because then you'll have accessed a protected class in a different package. You won't imported it yet. If ypu do import it correctly using

        Code:
         import com.dizzybird.test.*;
        Then you cannot say
        Code:
         Foo a = new Foo();
        But you are allowed to say
        Code:
        int i = Foo.i;
        Which is the point they were trying to make in the book.

        Comment

        • selimsivad83
          New Member
          • Dec 2006
          • 4

          #5
          hi again, what you are suggesting doesn't work. actually since the class has
          a more restricted access than the static member, the logical thing is to explicitly import the static part, which has the "correct" access specifier.
          anyway both ways give out the error that "access to the class is denied".
          in fact does the statement of the author sounds logical to u?
          if the class has package access how can the static member have public access?

          Comment

          • selimsivad83
            New Member
            • Dec 2006
            • 4

            #6
            just to be sure :)
            if the code below is what you're suggesting, it doesn't work
            Code:
            package com.dizzybird.test;
            
            class Foo
            {
            	public static int i = 1983;
            }
            Code:
            package com.dizzybird.test.anothertest;
            
            import com.dizzybird.test.*;
            
            class Test
            {
            	public static void main(String[] args)
            	{
            		int i = Foo.i;
            	}
            }
            com.dizzybird.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package

            Comment

            Working...