Results 1 to 17 of 17
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Private variables (attributes) in class / constructor

    When creating a class with a constructor, why does one have to create private variables (attributes) to be used as parameters by the object? The object's parameters will be set to be exactly equal to the private variables (attributes), so what is the point of having the private variables (attributes)?

    Why are both private variables (attributes) and parameters needed when they are set to be equal each other anyway?
    Last edited by Stud1; 01-07-2014 at 06:43 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Private variables (attributes) in class / constructor

    Can you give an example of exactly what you're talking about?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Private variables (attributes) in class / constructor

    Do you mean something like this?

    Java Code:
    private int x, y, width, height;
    
    public Block(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    If so, the reason is that you need to be able to use these variables in other methods inside the class. If you do not set them equal to each other, you can only use the parameters which can only be used in the constructor. Once you do this.x = x; you can use the x variable in other methods.

  4. #4
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by KevinWorkman View Post
    Can you give an example of exactly what you're talking about?
    Quote Originally Posted by pj6444 View Post
    Do you mean something like this?
    Yes, exactly like that.

    Quote Originally Posted by pj6444 View Post
    If so, the reason is that you need to be able to use these variables in other methods inside the class. If you do not set them equal to each other, you can only use the parameters which can only be used in the constructor. Once you do this.x = x; you can use the x variable in other methods.
    Ok, so you can only use the variables in other methods if they have been declared outside the constructor, correct?
    If so, why not only declare them outside the constructor?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by Stud1 View Post
    Ok, so you can only use the variables in other methods if they have been declared outside the constructor, correct?
    Yep. What we're talking about now is called scope, and is worth a google if you're confused.

    Quote Originally Posted by Stud1 View Post
    If so, why not only declare them outside the constructor?
    Let's expand our example:

    Java Code:
    private int x, y, width, height;
     
    public Block(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    
    public void draw(Graphics g){
       g.fillRect(x, y, width, height);
    }
    Now we set the x, y, width, and height variables in the constructor, and we use them in the draw() function. This would allow us to create a bunch of these objects at the beginning of our program, and then use them later when we want to draw them. Our draw code does not need to worry about setting the value of x, y, width, and height, it just uses whatever we set them to before.

    If we did not pass them into the constructor, we would have to know their values in our draw code, which we might not have.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    What if we only wrote this:

    Java Code:
    private int x, y, width, height;
     
    public Block(int x, int y, int width, int height) {
    
    }
    
    public void draw(Graphics g){
       g.fillRect(x, y, width, height);
    }
    It looks to me as if the constructor already got the same parameter values as the attributes. Or why isn't "int x" (wrriten as an attribute) already equal to "int x" written in the parameter? The constructor is in the same class as the attributes, so why would not variables with the same name and type be the same when they are already declared?

    Now we set the x, y, width, and height variables in the constructor, and we use them in the draw() function. This would allow us to create a bunch of these objects at the beginning of our program, and then use them later when we want to draw them. Our draw code does not need to worry about setting the value of x, y, width, and height, it just uses whatever we set them to before.

    If we did not pass them into the constructor, we would have to know their values in our draw code, which we might not have.
    Shouldn't the objects initially have no values at all? It would make more sense to me to give the object values at the moment of creating them in our draw code. Wouldn't this be more logical?

    Btw. when writing "this.x = x", which one is the attribute and which one is the parameter? if I understood correctly "this.x". is the attribute and "x" is the parameter. Correct?

    If we write this.x = x it means we take the attribute x and give it a value being equal to the parameter x. That means that the private variable x is changed through the change of the parameter value. But why is that good? What is the point of this?
    Last edited by Stud1; 01-07-2014 at 09:45 PM.

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by Stud1 View Post
    What if we only wrote this:
    Java Code:
    private int x, y, width, height;
     
    public Block(int x, int y, int width, int height) {
    
    }
    
    public void draw(Graphics g){
       g.fillRect(x, y, width, height);
    }
    Why couldn't this be enough?



    Shouldn't the objects initially have no values at all? It would make more sense to me to give the object values at the moment of creating them in our draw code. Wouldn't this be more logical?

    When writing "this.x = x", which one is the attribute and which one is the parameter? if I understood correctly "this.x". is the attribute and "x" is the parameter. Correct?

    If we write this.x = x it means we take the attribute x and give it a value being equal to the parameter x. That means that the private variable x is changed through the change of the parameter value. But why is that good? What is the point of this?
    You should look around google for the keyword 'this' it will really help in programming. As for your question, this.x is your attribute and x is your parameter.

    It is good because if you just ran that draw method without setting attributes to the parameters, nothing would happen, using the constructor so set values will allow you to draw with specific coordinates.
    Last edited by zFollette; 01-07-2014 at 09:45 PM.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Private variables (attributes) in class / constructor

    Java doesn't "know" that the values you're passing in correspond to the class variables, even if they have the same name. You have to do the assignment yourself.

    It might not make much sense to you because you haven't used objects enough. Honestly you might want to come back to this once you have some more practice.

    Consider you're on a programming team. The program involves a bunch of blocks bouncing around the screen. It's one person's job to create the blocks. It's another person's job to move the blocks around. It's your job to draw the blocks on the screen.

    In your code, you can simply call the draw() function of each block to tell the blocks to draw themsleves. You don't have to worry about where the block is (that's not your job) or setting the variables (that would interfere with other people's jobs).

    Also, this whole mess can be avoided by simply giving the parameters a different name:

    Java Code:
    int x;
    int y;
    int width;
    int height;
    
    public Block(int myX, int myY, int myWidth, int myHeight) {
      x = myX;
      y = myY;
      width = myWidth;
      height = myHeight;
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Private variables (attributes) in class / constructor

    You need to set them because if you don't the draw() method will be looking at the variables you declared at the top, which will have a value of null unless you set them with this.x = x etc.

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by pj6444 View Post
    You need to set them because if you don't the draw() method will be looking at the variables you declared at the top, which will have a value of null unless you set them with this.x = x etc.
    Close, but primitive data types can't equal null.

  11. #11
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by pj6444 View Post
    You need to set them because if you don't the draw() method will be looking at the variables you declared at the top, which will have a value of null unless you set them with this.x = x etc.
    The variables will get the 'default' values if they are not set in the constructor: objects will be null, numbers 0, boolean false and char '\u0000' .

  12. #12
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Private variables (attributes) in class / constructor

    My bad :P

  13. #13
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    Why do the attributes need to be private? As I understood it "private" means that their values can't be changed from outside the class. But here the private values will be determined by the constructor (i.e. this.x = x), whose values in turn will be determined when creating objects (which presumably will be done in another class). So in practice the "private" values are then determined from another class after all. So what was the point of setting them as private?

  14. #14
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by Stud1 View Post
    Why do the attributes need to be private? As I understood it "private" means that their values can't be changed from outside the class. But here the private values will be determined by the constructor (i.e. this.x = x), whose values in turn will be determined when creating objects (which presumably will be done in another class). So in practice the "private" values are then determined from another class after all. So what was the point of setting them as private?
    Only access to class members, the constructor initializes values, but it is not simply assigning most of the time.

    In this simple example, one can give whatever values and mess everything. Normally
    the methods would not allow illegal values. this is why they are private, because it is safe to give access through methods you have defined and control the values, while if they were public, one could
    change them without restriction. Read also about encapsulation.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by Stud1 View Post
    Why do the attributes need to be private? As I understood it "private" means that their values can't be changed from outside the class. But here the private values will be determined by the constructor (i.e. this.x = x), whose values in turn will be determined when creating objects (which presumably will be done in another class). So in practice the "private" values are then determined from another class after all. So what was the point of setting them as private?
    Like I said, I think you need to take a step back and use objects more. Asking us a million questions (especially without responding to any of the explanations you've already received) will take much longer than just practicing.

    But to answer your question, imagine again the scenario where you're on a programming team. It's one person's job to move the block around, which might be accomplished with setter functions:

    Java Code:
    public class Block{
       private int x;
       private int y;
       private int width;
       private int height;
    
       public Block(int myX, int myY, int myWidth, int myHeight) {
         x = myX;
         y = myY;
         width = myWidth;
         height = myHeight;
       }
    
       public void setX(int newX){
          x = newX;
    
          if(x is outside screen){
             move ball inside screen
          }
    
          if(x is hitting a wall){
             make ball bounce
          }
       }
    
       //similar function for Y
    
       public void draw(Graphics g){
          g.fillRect(x, y, width, height);
       }
    }
    Notice how nobody can change the x value except by calling the setter function, and the setter function does something with the new value every time it's called. Now, what if the x and y variables were public? What if you joined the team and you didn't really know what you were doing, and you accidentally set the x and y values to be outside of the screen by directly changing the variables instead of calling the setter?

    You've now broken the behavior of the program, and the guy in charge of moving the blocks has no control over it. If the variables were private, you would have known that you had to call the setter instead, which would guarantee that the bouncing code was triggered.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by KevinWorkman View Post
    Like I said, I think you need to take a step back and use objects more. Asking us a million questions (especially without responding to any of the explanations you've already received) will take much longer than just practicing.

    But to answer your question, imagine again the scenario where you're on a programming team. It's one person's job to move the block around, which might be accomplished with setter functions:

    Java Code:
    public class Block{
       private int x;
       private int y;
       private int width;
       private int height;
    
       public Block(int myX, int myY, int myWidth, int myHeight) {
         x = myX;
         y = myY;
         width = myWidth;
         height = myHeight;
       }
    
       public void setX(int newX){
          x = newX;
    
          if(x is outside screen){
             move ball inside screen
          }
    
          if(x is hitting a wall){
             make ball bounce
          }
       }
    
       //similar function for Y
    
       public void draw(Graphics g){
          g.fillRect(x, y, width, height);
       }
    }
    Notice how nobody can change the x value except by calling the setter function, and the setter function does something with the new value every time it's called. Now, what if the x and y variables were public? What if you joined the team and you didn't really know what you were doing, and you accidentally set the x and y values to be outside of the screen by directly changing the variables instead of calling the setter?

    You've now broken the behavior of the program, and the guy in charge of moving the blocks has no control over it. If the variables were private, you would have known that you had to call the setter instead, which would guarantee that the bouncing code was triggered.
    Thanks, that was a very good explanation. I did read the earlier replies also and I appreciate the answers even if I didn't responded to them. I learn quite well by asking these questions (maybe that's not true for everyone, we all have different ways of learning). I am of course also practising at the same time. But some things I have to ask about, especially if I don't even know how to practise them.

    I have some more questions, just to clarify. These are questions that I can't really "practise" myself to find out, since they are rather about how java is used by people who use it a lot and not by beginners.

    1. Do parameters of the constructor ALWAYS already exist as attributes of the class? Would there be any instance when they wouldn't be? If so, why?
    2. Same question for variables used in methods, do they ALWAYS exist as attributes in the class?

    3. Are attributes always used by either the constructor or another method? Or could there exist attributes that are never used either as parameters by the constructors or by any method? If so what would be the point of that?
    Last edited by Stud1; 01-08-2014 at 06:57 PM.

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: Private variables (attributes) in class / constructor

    Quote Originally Posted by Stud1 View Post
    1. Do parameters of the constructor ALWAYS already exist as attributes of the class? Would there be any instance when they wouldn't be? If so, why?
    2. Same question for variables used in methods, do they ALWAYS exist as attributes in the class?
    The answer in both cases is no. Consider the println() method. You supply it an argument to be printed, but the PrintStream object (of which System.out is an instance) does not save those arguments in class variables.

    Can *you* think of any examples?

    Quote Originally Posted by Stud1 View Post
    3. Are attributes always used by either the constructor or another method? Or could there exist attributes that are never used either as parameters by the constructors or by any method? If so what would be the point of that?
    One thing that comes to mine is the serialUID, but that's a corner case. Most attributes are used somewhere, otherwise why are they there in the first place? But there isn't anything stopping you from having an attribute that you never use- in fact, I can see this happening if code gets commented out for testing purposes. (Actually you set it so you get warnings for unused values, but warnings don't stop you from doing anything).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 4
    Last Post: 08-26-2012, 02:09 AM
  2. Why private variables
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 06-07-2012, 10:11 PM
  3. Replies: 2
    Last Post: 06-03-2011, 10:15 PM
  4. Why make class variables private?
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:01 PM
  5. Replies: 3
    Last Post: 03-14-2009, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •