By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,469 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,469 IT Pros & Developers. It's quick & easy.

public static in package access class

P: 4
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.test;

class Foo
{
public static int i = 1983;
}

-------------------------------------------------------------------------------------------------
package com.dizzybird.test.anothertest;

import static com.dizzybird.test.Foo.*;
// import com.dizzybird.test.*;

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

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

Test.java:11: com.dizzybird.test.Foo is not public in com.dizzybird.test; cannot be accessed from outside package
System.out.println(Foo.i);
^
1 error
-------------------------------------------------------------------------------------------------
Any ideas on that?
Thanks for reading my question.
Dec 7 '06 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
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.test;

class Foo
{
public static int i = 1983;
}

-------------------------------------------------------------------------------------------------
package com.dizzybird.test.anothertest;

import static com.dizzybird.test.Foo.*;
// import com.dizzybird.test.*;

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

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

Test.java:11: com.dizzybird.test.Foo is not public in com.dizzybird.test; cannot be accessed from outside package
System.out.println(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

Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.*;
and you will see what they are talking about.
Dec 7 '06 #2

P: 4
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
Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.*;
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.Foo.i;
  2.  
if the class declaration is like this
Expand|Select|Wrap|Line Numbers
  1. class Foo
  2. {
  3.     public static int i;
  4. }
  5.  
in the case i referred to, test is a package so what you suggested won't work.
Dec 7 '06 #3

10K+
P: 13,264
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
Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.*;
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.Foo.i;
  2.  
if the class declaration is like this
Expand|Select|Wrap|Line Numbers
  1. class Foo
  2. {
  3. public static int i;
  4. }
  5.  
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

Expand|Select|Wrap|Line Numbers
  1.  import com.dizzybird.test.*;
Then you cannot say
Expand|Select|Wrap|Line Numbers
  1.  Foo a = new Foo();
But you are allowed to say
Expand|Select|Wrap|Line Numbers
  1. int i = Foo.i;
Which is the point they were trying to make in the book.
Dec 7 '06 #4

P: 4
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?
Dec 7 '06 #5

P: 4
just to be sure :)
if the code below is what you're suggesting, it doesn't work
Expand|Select|Wrap|Line Numbers
  1. package com.dizzybird.test;
  2.  
  3. class Foo
  4. {
  5.     public static int i = 1983;
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. package com.dizzybird.test.anothertest;
  2.  
  3. import com.dizzybird.test.*;
  4.  
  5. class Test
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         int i = Foo.i;
  10.     }
  11. }
  12.  
com.dizzybird.test.Foo is not public in com.dizzybird.test; cannot be accessed from outside package
Dec 7 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.