• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between access modifiers for variables/classes  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I have to ask this: What is he difference between public and private variables? How does it affect the way they are used, if that's the right way for me to ask it, and can you give me some examples to help me understand?

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this tutorial will explain it better than I could.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Public variables can be accessed by any class; private variables can only be accessed in the same class they're declared.

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Public variables can be accessed by any class; private variables can only be accessed in the same class they're declared.



proctected: subclasses and classes in the same package
package: classes in the same package

Note that the package modifier is actually an empty modifier.

Example:
boolean myMethod()

Is using the package modifier
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Example:
boolean myMethod()

Is using the package modifier

No, this method does not use any modifier.
No modifier means package access level.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote: . . .
proctected: subclasses and classes in the same package
. . .
The rules about protected are slightly more complicated than that. It is defined as in the same package or in any code responsible for implementing the instance. So in subclasses, static methods don't constitute part of the instance, and (I think: you would have to check) protected members are not accessible.

Beware: that link may be difficult to read.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgive me, but I'm still unclear. What do you mean by 'can be accessed by any class'? Does that mean, say, that I can put that variable in another method, which is in another class and it will know what its value is? Not sure I'm wording this right.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote: . . . I can put that variable in another method, which is in another class and it will know what its value is? . . .
If “that variable” is a public variable of a public class anywhere in your classpath, yes.

Unfortunately it means your code can also change the value of that variable without the “home” object "#c201c;knowing” it is happening. So public variables are bad news. Until proven otherwise, give all variables private access. You may even need to copy variables as the come into your object or go out of it.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, Campbell Ritchie, can you give me a code example of that? And can you remind me what the classpath is? Sorry, but I haven't been here in a while.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit: add following]
Classpath = anywhere you can find other classes to use for your Java® app. Defaults to current location and in standard Java® installation but you can change that when you run your app.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell in heading (1.6.2.1)of the link given by you

Don't you think they should have mentioned that class S should be outside the package in which C is present
(To make it more clear)
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the preceding section they say this about protected access:-
  • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
  • Access is correct as described in §6.6.2.
  • Which means it is not actually necessary to say anything about different packages.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct me if I am wrong

    Member or constructor,which is declared protected in C(class)
    Can be accessed by:
    1-all the classes within the same package ,in which C is present

    2-only subclasses, outside that package.


    While accessing them by the subclass outside the package, then they can't be accessed by creating instance of C

    Like C.Id

    They can only be treated as if they belong to subclass
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think that is right, yes.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jls are horribly tough to understand it in the first time of reading
     
    Christopher Laurenzano
    Ranch Hand
    Posts: 105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, Campbell Ritchie or anyone else -- I'm getting a better idea, and it's been a few days since I've been here.

    Could you give me a description of what's happening in your code examples? I apologize but it's not quite clear yet. What's happening in those examples? I'm still a bit of a newbie.
     
    Tim Harris
    Ranch Hand
    Posts: 57
    3
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Christopher Laurenzano wrote:Okay, Campbell Ritchie or anyone else -- I'm getting a better idea, and it's been a few days since I've been here.

    Could you give me a description of what's happening in your code examples? I apologize but it's not quite clear yet. What's happening in those examples? I'm still a bit of a newbie.


    I'm a big fan of code examples that can relate to real-life situations. So let's look at the following block of code:


    You walk into a store. The store owner greets you at the door. You say hello back, and you ask for the store owner's name. He (or she) gives it to you without any problems. Makes sense, right? It's handy to know their name, especially if you want their help for something. From a code perspective, this would be like another class (say a Customer class) making a call to the instance of a Store class, and asking for the store owners name.

    But what if you wanted to know his salary? Some people aren't comfortable with giving out that information! So, if you tried to access it directly as a Customer, you'd be denied.

    In its most simplistic explanation, private means only accessible to that class, whereas public means accessible by any class.
    With me so far? Good. Now it gets tricky - let's say you needed to get that salary. Maybe you're an accountant. But, oh noes! salary is a private variable!

    What can we do?

    Well, suppose the code looked something like this:


    How does this work? Because the getSalary() method is public, you can use it to access what is typically only accessible to that class.
    Important note: Setting all your variables to private, and having them be accessed by getter and setter methods (such as getSalary and setSalary) are considered best practices when programming in Java - it helps you maintain control over how variables are accessed by other classes.

    Hope this helps!
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Agree with the last bit; if you make the name public you can easily run this sort of codeAnd there can be nothing worse than that.
     
    Tim Harris
    Ranch Hand
    Posts: 57
    3
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Agree with the last bit; if you make the name public you can easily run this sort of codeAnd there can be nothing worse than that.


    I don't know. I could imagine worse things than being named Campbell. ;)

    All joking aside, you're absolutely right. Security is a tricky thing - too much access and people will be doing things you don't want. Too little access and people won't be able to do what they need to do. Knowing how to use public and private modifiers will help you out a lot.

    One other piece of advice, is until you get the grasp of public and private, don't worry about protected or default (which is no modifier). They'll confuse you, and when you're starting out you're probably not going to need to worry about those modifiers anyway (I work with Java every day and I still don't worry about them.)
     
    Christopher Laurenzano
    Ranch Hand
    Posts: 105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand the difference between them using setters/getters. What I meant in reference to the code example is what does the package and import statement do? can you tell me what's happening in the lines of code?

    And what's happening in line 8 of the second example?

    If I wanted to use the value of a variable from one class in another class, is it possible to do that?

    Sorry if my questions are redundant; just trying to get a grasp.
     
    Tim Harris
    Ranch Hand
    Posts: 57
    3
    Chrome Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Christopher Laurenzano wrote:What I meant in reference to the code example is what does the package and import statement do? can you tell me what's happening in the lines of code?

    Package statements are used to bundle bits of your code together for readability and avoiding naming conflicts (though the article explains it better than I can.)
    Import statements allow you to reference code from other packages without explicitly naming the package every time. For example,

    Lets you use write code like this:

    Instead of like this:

    It makes the code much more readable.

    Christopher Laurenzano wrote:And what's happening in line 8 of the second example?


    You mean this?

    When you see the new keyword, that means an instance of that class is being created in memory - in this example, an object named f of class Foo. The "123" is an argument passed in to the constructor.

    Christopher Laurenzano wrote:If I wanted to use the value of a variable from one class in another class, is it possible to do that?

    Absolutely! The safest way to do this (as mentioned before) is to make the variable itself private, but have a getter method that lets you access that variable (see my example).
     
    Campbell Ritchie
    Marshal
    Posts: 56529
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Christopher Laurenzano wrote:. . .
    If I wanted to use the value of a variable from one class in another class, is it possible to do that?
    . . .
    Sorry for not replying earlier.

    Don't think about variable in a class. Think about the variables being in an object. TH has already told you how you can use a variable from one object in an object of a different type.
     
    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
    Christopher Laurenzano,

    Since your followup question is on a completely different topic, your post was moved to a new topic.
    (This informational message will self destruct in two days)

    Henry
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!