Home arrow static arrow Java Programming [Archive] - java compiler contradicts Java book
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java compiler contradicts Java book
This topic has 5 replies on 1 page.

Posts:28
Registered: 6/28/04
java compiler contradicts Java book  
Jun 28, 2004 8:13 PM



 
NOW this is a big surprise...One of the Java book says "If a static member of the class, singleton in the ex below, is PUBLIC, the client (HelloUser in this case) can still access the static member even if the client is not in the same package" . I tried it but got the following error - singleton can't be accessed outside the package ? Any clues ?

import learn.*; public class HelloUser {    public static void main(String args[])    {	Object obj[] = {new Integer(11), new Character('A')};	Hello.print(obj);		singleton.GetSingleton().print();    }}


package learn; class singleton {    private static int mX;    private singleton(int x) {mX = x;}    private static singleton sSingle = new singleton(10);;    public static singleton GetSingleton() {	print();	return sSingle;    }    public static void print()    {	System.out.println("Val: " + mX);    }}
 

Posts:8
Registered: 6/3/04
Re: java compiler contradicts Java book  
Jun 28, 2004 8:57 PM (reply 1 of 5)



 
Since the singleton class is not defined as public, all the public methods of this class ( regardless of whether they are static or not ) will be visible only inside the learn package.

For more information read http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Can you post the exact wordings from your JAVA book?
 

Posts:28
Registered: 6/28/04
Re: java compiler contradicts Java book  
Jun 28, 2004 9:05 PM (reply 2 of 5)



 
yeah I knew that but this book talk specifically abt public static

It says "An object of a class with package access can be created by any class inside thw package but not outside the package. However, if a static member of that class is public, the client programmer can still access that static member even though they can't create an object of that class.
 

Posts:1,183
Registered: 1/23/02
Re: java compiler contradicts Java book  
Jun 28, 2004 9:24 PM (reply 3 of 5)



 
The book is wrong. [url http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#102765]JLS 6.6.1[/url] :

If a top level class or interface type is not declared public, then it may be accessed only from within the package in which it is declared.

A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access ...
 

Posts:4,500
Registered: 17.04.98
Re: java compiler contradicts Java book  
Jun 29, 2004 6:02 AM (reply 4 of 5)



 
The book is wrong. [url
http://java.sun.com/docs/books/jls/second_edition/html/
ames.doc.html#102765]JLS 6.6.1[/url] :

No, the book is not wrong just not quite complete.
It should have stated that other classes in the same package have access ...
 

Posts:1,183
Registered: 1/23/02
Re: java compiler contradicts Java book  
Jun 29, 2004 7:45 AM (reply 5 of 5)



 
No, the book is not wrong just not quite complete. It should have stated that other classes in the same package have access ...

OK, let's complete the original statement using your suggestion :

An object of a class with package access can be created by any class inside the package but not outside the package. However, if a static member of that class is public, the client programmer can still access that static member from withn the same package even though they can't create an object of that class.

Does this make any sense? No.
 
This topic has 5 replies on 1 page.