• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about constructors & "private"  RSS feed

 
henry xiao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the example code that I'm reading in my book:


In line 17, I'm confused as to why the BMI method has another constructor - what is the purpose of this? Also, why are the declarations at the beginning private for the most part? I understand that it is to protect their contents (or something?) but I'm having a hard time understanding the fundamentals behind the terms static/private. Could anyone explain it to me like I'm 8? Thanks in advance.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 17, I'm confused as to why the BMI method has another constructor - what is the purpose of this?

To start with its not a method but a constructor. What it essentially means is if there is no age provided, it will default to 20.

Also, why are the declarations at the beginning private for the most part? I understand that it is to protect their contents (or something?) but I'm having a hard time understanding the fundamentals behind the terms static/private.

That's encapsulation. You do not want anyone messing around with those values, without your permission.
Imagine a User object, which has a variable called Date dateOfBirth; In real life, once set, this value will never change. To enforce this, you would declare it as private and not provide any setter method. In general it is always a good idea to declare instance variables (like the dateOfBirth) private and provide getter/setter methods only when required.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the matter of Static /private, if a variable is static it can not have instances or better say it will be same for all the instances of an Object.
For example,When you write

It will give you an output
name
0.45359237
NewName
0.45359237

As you can see the content of static variable will be same for both objects. Try to read about encapsulation as it will answer the need of creating
getters and setters; and creating declaring variables as private.
 
henry xiao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An additional question: why is BMI() not a method but a constructor? I'm a bit confused because it resembles a "set" method to me. Also, if in the BMI constructor instead of (this.name = name;) it had (name = name;) would this not work because the name string is declared private? Would it work if it were public? Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is BMI() not a method but a constructor?

Methods have return types (including void which indicates it does not return any value). Constructors dont.
As per the specification the constructor takes the format
accessModifier NameOfTheFileWithMatchingCase

Also, if in the BMI constructor instead of (this.name = name;) it had (name = name;) would this not work because the name string is declared private? Would it work if it were public?

Easy enough to try it out yourself. Does it work?
The private modifier does not come into the picture in this case, because you are trying to manipulate the value from inside the same class, which is allowed.

 
henry xiao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I made a main method that creates a BMI object and uses the getBMI() method. Without the "this" statement the BMI fails to compute, but with the "this" statement it computes correctly. I'm assuming, like you said, because I made a new class, the method failed to work because it didn't incorporate a self-reference and because the declarations were privates? Could you correct or expand on this? Thanks for your help btw!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!