• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is Default Package included in the Package Level Access?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know how the default package is defined in Java.I know how public and private access is defined but I don't know whether there is any default package access that is defined in package level access in java.

I'm asking this question because when I compile java file containing two classes since no modifier is given for classes,java compiler would give package level access as the default access modifier for the classes.Since no package is defined,java compiler would use the default package but I couldn't get whether the default package is included in package level access in java.Could anyone help me.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use of the default package is discouraged. A lot of Java tools don't like it when a class isn't part of an explict package.

As far as default scope for class members, whether the class is in a named package or the default package doesn't matter. All classes within that package have access to them.
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Holloway:Thanks.D
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Holloway:Sorry I accidentally pressed the enter key while typing 'D'.Anyway did you meant to say that the classes in an unnamed package can access any other member variables if there members are public?
Consider this code:


Can Class B and A access each other's members without creating an object of either of them.Could you help me and this where I'm stuck at.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds peculiar. You cannot access instance fields of any class unless you create an instance. Are you getting confused about something?
You can read about the access modifiers here in the Java® Tutorials, and access in the unnamed package is no different from any other package.
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie: I'm getting stuck about the term access.Before that to check whether a variable could be accessed how is the priority of access levels defined?What really confuses me is since there are different types of access levels which should we take first?

Accesses are:

1.Package level
2.Class level
3.Member access label(I'm not sure about this but I think this comes from the method access label isn't it?

So could you tell me which access mentioned above should be taken first for accessing an object?Also when it comes to access:does access means:

1.'just(without instant of a class) using an object(variable)' or
2.does it mean 'only after creating an object of the class(where object resides) accessing an object'?

Also could you tell me how this applies for both an static as well as for an non-static members(Expecting 4 access statements all together-2 for each mentioned above).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't a “priority” of access levels. Did you read the Java® Tutorials link I gave you? You are getting confused between accessing things and different kinds of members. Go back to that link and then explain what you don't understand. I am having difficult understanding your question.
The bit about top‑level access means tha top‑levet classes are either public or package‑private. I think package‑private access is officially called default.

What is a member access label? I have never heard of it.
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie:Sorry for mentioning member access label I meant to say about access modifier.The place where I'm getting confused is:which access should we consider while considering a member.

It's natural for an Java program to have classes,packages etc.Also it's definite that there are access defined for each of these(class,package).Even as you have suggested to look in Java Tutorials it just gives you how an access for each entity is defined and sure it gives you an example but that too they haven't specified the access modifiers for any class and no methods are mentioned.I would like to know about classes without packages because that's where I'm starting coding from.Could you help me.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Johns wrote:It's natural for an Java program to have classes,packages etc.Also it's definite that there are access defined for each of these(class,package).


Actually, it isn't. The only things you define access for are classes, methods, and fields (the last two being sometimes collectively referred to as class "members").

A package is simply a place where you put classes - you can't define an "access" for it - and it basically equates to a directory on your file system. This is slightly muddied when you create jars; but since they are basically glorified Zip files, you can think of them as a directory tree added to your "current" directory.

There are four "access" qualifiers (although I prefer to think of them as "visibility" qualifiers):
1. private - visible only inside the same class.
2. Default (ie, no qualifier) - visible only inside the same package (ie, directory).
3. protected - visible only inside the same package OR a subclass (ie, any class that extends the one that defines the member).
4. public - visible to the world.
and only (2) and (4) apply to class definitions (unless you're defining a "nested" class).

So, if you define a private method or field, you will only be able to call it (or use it) from inside the same class; and if you use "default access", only classes in the same package will be able to "see" it.

And the general rule of thumb is not to make things any more visible than they need to be. So:
1. Always, always, always make your fields private.
2. Don't make a method public unless you KNOW that everyone is going to need to use it.
3. To begin with, most of the classes you write will be public; but as you get more experienced (and your projects get bigger) you may well find a use for writing "helper" classes that are only visible from within the same package.

To be honest, I've only rarely found much use for the "default" visibility. 90% of the stuff I write is either public or private, with a preference for the latter.

As for the rest ... that's what the tutorials are for.

HIH

Winston
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston Gutkowski:That's a good answer.But I still can't get what you mean by "only classes in the same package will be able to see it".Does that mean you could access an variable in a class from another class provided both classes are in the same package?That's only where I'm stuck at.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Johns wrote:Does that mean you could access an variable in a class from another class provided both classes are in the same package?


Yes. And since packages are usually written by a single programmer or department, the default access is a way of saying:
I only want classes that I (or 'my department') wrote to be able to "see" this.

On the other hand, any variable other than a private one is a potential source for errors, since anyone who can "see" it can also CHANGE it.

So, if you plan on making a field anything other than private, make it final as well.

HIH

Winston
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston Gutkowski:Now I got what you mentioned.Should I now resolve this answer?The reason is I executed a code having two classes in the same package(so definitely should be same without package isn't it?) and realized that if the member is static you could call it by classname.member and if it's not static you should create the object before accessing it using objectname.member.Should I post the code before resolving it?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Johns wrote:@Winston Gutkowski:Now I got what you mentioned.


Well done!

Should I now resolve this answer? ... Should I post the code before resolving it?


1. If you feel your question has been answered - yes.
2. Entirely up to you; although we like people to do it if you think it will help others. After all, that's what we're here for.

Winston
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So finally I got how we could access an variable in another class without creating an object provided both classes rest in the same directory.Just make the variable static.

Here's the code:





This shows that classes residing in the same package can access each other's members provided it is static even though it is not public because default package access comes to play.You can do this with non static modifiers too but you have to declare an object of the class to access it's member using objectname.classname.So we could see that default package is included in package level access.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . .
A package is simply a place where you put classes - you can't define an "access" for it . . .

…because the Java® Language Specification has already defined its access. It is equivalent to public everywhere.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Johns wrote:So finally I got how we could access an variable in another class without creating an object provided both classes rest in the same directory.Just make the variable static.


Nooooo. That is NOT the answer.

In fact, static variables of ANY type are never the answer - even if it might seem like it.

So: find another way of doing what you want (or what you think you want)... NOW.

Hope you had a good Christmas.

Winston
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston Gutkowski:Perhaps I just wanted to know whether classes in the same directory without a package name(so takes the default package) are included in the package level access.So what I have realized is that you could access 'static' members without objects and 'non-static' members with objects(if they are present in the same directory) which clearly states that access for members in a unnamed package are also defined under package level access.If my answer is still wrong could you suggest how should I edit the post to make it correct.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, just to clarify things, there are two "defaults" in action here.
- default access modifier
- use of the 'default' (i.e. no) package.

The default access modifier means that any classes in the same package can access them. This applies to all classes - even those with 'no' package.
The default package, is a package, so any class in the default package can access non-private variables and methods of any other class in the default package.

To improve your statement:
"This shows that classes residing in the default package can access each other's members because of default package access. So the default package is included in package level access rules"

The most confusing part of your answer was your reference to static variables. Static/non-static is irrelevant to the access modifiers - as you yourself have pointed out, you can access both. Better to leave it out of the statement entirely.


As an aside, there is one interesting caveat about using the default package: Since Java1.4, classes in the default package can NOT be imported by any classes in a named package.
Best practice (as has been already mentioned) is to always write your class within a package, which kinda makes your initial question a moot point.



 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So finally I got how we could access an variable in another class without creating an object provided both classes rest in the same directory.


By executing this code I could access the variable a in Class B from Class A.
This shows that classes residing in the default package can access each other's members because of default package access. So the default package is included in package level access rules.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Johns wrote:So finally I got how we could access an variable in another class without creating an object provided both classes rest in the same directory...


Well done. However, I say again: that is generally a BAD way to go. Indeed, static variables are almost always the wrong way to do things.

Reason: In addition to being able to access the variable, any class that can "see" it can also change it, viz:
  A.a = 7;
which may be a bit of a surprise for any other code that expects it to be 5 (or not to change).

So: if you define any static field that is not private: always, always always make it final.

HIIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . if you define any static field that is not private: always, always always make it final. . . .

And it is probably best to make it immutable (if it is a reference type), too.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic