• Post Reply Bookmark Topic Watch Topic
  • New Topic

Queries regarding access modifiers  RSS feed

 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’m newbie to Java. I’ve following doubts :

1) I know that access modifiers (public, protected, default) are used with instance variables and methods to control accessibility of a class. But I’ve seen many a times access modifier ‘public’ is prefixed with keyword ‘class’ in class declaration, for e.g. – Even when class is public and the fields are private, we cannot access the class from space outside it. So what is the use of writing ‘public’ modifier before a class ?
So when access modifiers used with instance variables and methods control entire the accessibility of a class why sometimes ‘public’ is prefixed with a class declaration ? What does it mean ? Also why only public , as when used other access modifiers (private ,protected) with class declaration it gives an error.

2) In reference to the above question I’ve another doubt regarding following code (just for checking the working of modifier 'public') :

If I name this file other than 'A' ( A is name of public class in this code ), lets say Sample.java and compile it in JCreator, it gives an error saying – “class A is public, should be declared in a file named A.java”. What does it mean. Also when I removed the modifier public before class ‘A’ , it compiled successfully even when the file name is other than that of class A. Please clarify this.



 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:...Even when class is public and the fields are private, we cannot access the class from space outside it. So what is the use of writing ‘public’ modifier before a class ?

What do you mean by "from space outside it"?
"public" classes are accessible from inside/outside the package. But they are not accessible from another JVM or I would say from another Java process.
"private" fields of classes cannot be accessed directly from any other class. They can be accessed through methods.

Dev Choudhary wrote:If I name this file other than 'A' ( A is name of public class in this code ), lets say Sample.java and compile it in JCreator, it gives an error saying – “class A is public, should be declared in a file named A.java”. What does it mean. Also when I removed the modifier public before class ‘A’ , it compiled successfully even when the file name is other than that of class A. Please clarify this.

Because this rule is enforced by the creators of Java language.
A .java file can have at most 1 top level public class or interface.
Name of the .java file has to be the same as the 1 top level public class.
If there is no top level public class in .java file, compiler does not enforce the rule, because then that class cannot be executed independently.

And the code you have provided

Even if you will be able to compile it providing the file name as A.java, you will not be able to run it.
Because JVM searches for a public static void main(String[] args) method in the class which we run.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote: . .
If there is no top level public class in .java file, compiler does not enforce the rule, because then that class cannot be executed independently.
. . .
Even if you will be able to compile it providing the file name as A.java, you will not be able to run it.
Because JVM searches for a public static void main(String[] args) method in the class which we run.
If there is no top‑level class/interface/enum in a .java file, then the file must be empty of all code.

You can run that code simply by writing
java B

There is a related thread active at the moment: please read this because you may find it helpful.
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say class X is accessible from another class Y which means that instance variables and methods of class X are accessible from class Y , right ? A public class (class prefixed with public modifier) is accessible from any space outside that class. But is this true even when the instance variables and methods of public class are declared as private ? That means is that class still acessible from the space outside that class ? If no, then what is the use of applying public modifier to the class ? If yes then does that means the public modifier of the class overrides the private modifiers of its instance variables and methods ?
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:Lets say class X is accessible from another class Y which means that instance variables and methods of class X are accessible from class Y , right ?
No.
If instance variables and methods have "private" modifier, these cannot be accessed directly (can be via public methods, which are called getters, setters) from other class, even though, if class itself is visible. Maybe example would help:

Dev Choudhary wrote:A public class (class prefixed with public modifier) is accessible from any space outside that class.
Don't use "space", it is rather confusing. In Java you should be talking about packages. I think you could find information here (<- link). Or google about "access modifiers" in Java (class modifiers, outer class in your case actually).
But is this true even when the instance variables and methods of public class are declared as private ?
Code example above should answer this. What do you think how it is?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eariler, I wrote: . . .
If there is no top‑level class/interface/enum in a .java file, then the file must be empty of all code.
. . .
But I see I hadn't read Tapas Chand's post correctly; he said public.

Sorry for my mistake
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are more details in the Java® Language Specification, but that can be very difficult to read.

As I said earlier, there is another thread about a very similar subject. Please read that. Link copied from one of my previous posts.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But I see I hadn't read Tapas Chand's post correctly; he said public.

Sorry for my mistake


Oh it is absolutely no issue.
You did not notice "public" that is why I could read this rather interesting thread. Thank you
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dev
In simple ways just consider this
For top level class(using word top to distinguish from inner class)
can have only 2 access modifier:
public
package-private (also known as default)
If you use public,your class is accessible by all classes inside your public class package or outside it
If you use package-private,your class is accessible by all classes inside your public class package but not outside the package

Now inside class instance variable, methods (both these can be combined as class member)and one component of class "Constructor" can have access modifier

public
package-private
private
Protected


public and package-private work as same as it work for top level classes

private members or constructor can be accessed only within same class
protected members or constructor can be accessed by all classes inside the package containing the class with protected members or constructor or by subclasses outside package(but like as members belongs to subclasses only)

If you have public class with private fields and public getters and setters,you can still access those private field outside the class with the help of getters and setters

This was all for access modifier

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally after 208 message posted
And 29 topics created, I finally got a cow.
Can I know who gave me the cow
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys.

I’m cleared with access modifier used for class members (instance variables and methods) but I’m sorry, there is still some confusion regarding access modifiers used for top level classes.
1) Can access modifier public or default be used only with top level classes or even with nested classes?
2) Lets say there are two different classes A and B, both in different packages. Then in order to have access to class A by class B , then both top level class, that is ‘A’ as well as class members should be defined public, right ? If either class or class members are not public then class B will not have access to class A, right ?

3) Consider following two codes :



I know that a super class reference variable can refer to sub class. In above scenario, even when instance variable ‘k’ of super class A1 is accessed in sub class A2 with reference variable of super class i.e of ‘A’ it gives error saying - <identifier> expected k=k++;
Why is this happening ?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:

I know that a super class reference variable can refer to sub class. In above scenario, even when instance variable ‘k’ of super class A1 is accessed in sub class A2 with reference variable of super class i.e of ‘A’ it gives error saying - <identifier> expected k=k++;
Why is this happening ?


Java statements can only be in constructors, initializers, or methods. You are not allowed to place statements anywhere in your code.

Henry
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Thanks.
But can anyone please reply to my other doubts in my previous post ?
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dev Choudhary wrote:OK. Thanks.
But can anyone please reply to my other doubts in my previous post ?
It seems all doubts were cleared up by above posts.
Please state again, what is not clear still and not answered above?
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Dev Choudhary wrote:OK. Thanks.
But can anyone please reply to my other doubts in my previous post ?
It seems all doubts were cleared up by above posts.
Please state again, what is not clear still and not answered above?


Some confusion regarding access modifiers used for top level classes.

1) Can access modifier public or default be used only with top level classes or even with nested classes?

2) Lets say there are two different classes A and B, both in different packages. Then in order to have access to class A by class B , then both top level class, that is ‘A’ as well as class members should be defined public, right ? If either class or class members are not public then class B will not have access to class A, right ? Please state that if these satements are true or else correct me if they are not.

 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1-In Java nested classes are considered members of their enclosing class. Thus, a nested class can be declared public, package (no access modifier), protected and private . Therefore nested classes can be inherited by subclasses of enclosing class.

2-why not consider like this
If you have public class you make it visible for all class (either inside or outside package)

a-Now if you have public fields and public method you can access them directly within other class(either inside or outside package)
b-if you have package-private field and public method you can access field directly within other class which is inside package via method anywhere you wish
c-if you have private field and public method than you can access field in other class only via method (either it is inside or outside package)
d-if you have protected field and public method you can access field directly by class inside package and by subclasses outside package but like it only belongs to subclasses and via method anywhere
you wish

Now can you work out what it will be if top class is package-private?
 
Dev Choudhary
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...
If top class is package-private, I think only public, default and protected class members are directly accessible by the classes in the same package but not in the other packages. The private fields can accessed by the other classes in the same package through public, package-private or protected methods of top class. Does I'm right ?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spot on
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!