473,804 Members | 3,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

public static in package access class

4 New Member
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.
Dec 7 '06 #1
5 3135
r035198x
13,262 MVP
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

Expand|Select|Wrap|Line Numbers
  1. import static com.dizzybird.test.*;
and you will see what they are talking about.
Dec 7 '06 #2
selimsivad83
4 New Member
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
r035198x
13,262 MVP
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
selimsivad83
4 New Member
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
selimsivad83
4 New Member
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.t est.Foo is not public in com.dizzybird.t est; cannot be accessed from outside package
Dec 7 '06 #6

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

Similar topics

9
4997
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
13
7737
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
6
8308
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream fStream=null; lock(fStream) //just in case we use it for multithreading to be thread safe {
6
3593
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
5
6804
by: rettigcd | last post by:
I have several classes that all have the same static member: class A{ public static string Table = "TableA"; } class B{ public static string Table = "TableB"; }
8
3954
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For example: class A { public: const vector<int>& getTable() { return m_table; }
9
2360
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice = CDec(txbInv.Text) Wage = CDec(txbTotWage.Text)
17
1451
by: Gerrit | last post by:
Hallo, In my application, the user must logon with a username and a password. In all the tables in my database, there is a field user. By saving new or changed records, the username is writed to that field. Now I have a class Session with the User-information. By opening the program, I have code like this on the mainform: Session sess = new Session().
0
1536
by: Mikhail Teterin | last post by:
Hello! I have a method, that is supposed to set fields in an object passed to it. Any object -- the names of the fields are determined at run-time, the only (obvious) requirement is, the fields must be "public". This worked perfectly fine, until I put that method's class into a package of its own. Now, when the method is given an object of a class /from another package/
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.