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.
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.
Comment