Method on how to access a variable?
I'm currently learning Java, and one of the examples in the textbook is as follows:
class Circle{
private double radius;
private double area;
public void setRadius(double r){
radius = r;
setArea(Math.PI * radius * radius);
}
public void setArea(double a){
area = a;
}
Looking at the setRadius method, is there a style preference/difference between writing what's above VS writing the following?
public void setRadius(double r){
radius = r;
setArea(Math.PI*r*r);
}
One of my classmates is making the argument that one should use the parameter r
instead of the private variable radius
because a public method shouldn't have direct access to a private variable unless it's by using a getter. Is this more of a style preference or is he right in that public methods directly accessing private methods is bad?
java
|
show 1 more comment
I'm currently learning Java, and one of the examples in the textbook is as follows:
class Circle{
private double radius;
private double area;
public void setRadius(double r){
radius = r;
setArea(Math.PI * radius * radius);
}
public void setArea(double a){
area = a;
}
Looking at the setRadius method, is there a style preference/difference between writing what's above VS writing the following?
public void setRadius(double r){
radius = r;
setArea(Math.PI*r*r);
}
One of my classmates is making the argument that one should use the parameter r
instead of the private variable radius
because a public method shouldn't have direct access to a private variable unless it's by using a getter. Is this more of a style preference or is he right in that public methods directly accessing private methods is bad?
java
2
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
6
InsetArea
you have a bug; it should bethis.area = a;
notarea = area;
– Elliott Frisch
Nov 13 at 3:55
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
5
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
2
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59
|
show 1 more comment
I'm currently learning Java, and one of the examples in the textbook is as follows:
class Circle{
private double radius;
private double area;
public void setRadius(double r){
radius = r;
setArea(Math.PI * radius * radius);
}
public void setArea(double a){
area = a;
}
Looking at the setRadius method, is there a style preference/difference between writing what's above VS writing the following?
public void setRadius(double r){
radius = r;
setArea(Math.PI*r*r);
}
One of my classmates is making the argument that one should use the parameter r
instead of the private variable radius
because a public method shouldn't have direct access to a private variable unless it's by using a getter. Is this more of a style preference or is he right in that public methods directly accessing private methods is bad?
java
I'm currently learning Java, and one of the examples in the textbook is as follows:
class Circle{
private double radius;
private double area;
public void setRadius(double r){
radius = r;
setArea(Math.PI * radius * radius);
}
public void setArea(double a){
area = a;
}
Looking at the setRadius method, is there a style preference/difference between writing what's above VS writing the following?
public void setRadius(double r){
radius = r;
setArea(Math.PI*r*r);
}
One of my classmates is making the argument that one should use the parameter r
instead of the private variable radius
because a public method shouldn't have direct access to a private variable unless it's by using a getter. Is this more of a style preference or is he right in that public methods directly accessing private methods is bad?
java
java
edited Nov 13 at 3:56
asked Nov 13 at 3:48
Tyler
147211
147211
2
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
6
InsetArea
you have a bug; it should bethis.area = a;
notarea = area;
– Elliott Frisch
Nov 13 at 3:55
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
5
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
2
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59
|
show 1 more comment
2
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
6
InsetArea
you have a bug; it should bethis.area = a;
notarea = area;
– Elliott Frisch
Nov 13 at 3:55
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
5
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
2
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59
2
2
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
6
6
In
setArea
you have a bug; it should be this.area = a;
not area = area;
– Elliott Frisch
Nov 13 at 3:55
In
setArea
you have a bug; it should be this.area = a;
not area = area;
– Elliott Frisch
Nov 13 at 3:55
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
5
5
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
2
2
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59
|
show 1 more comment
2 Answers
2
active
oldest
votes
In this case both method call are correct
setArea(Math.PI * radius * radius);
and
setArea(Math.PI*r*r);
public method shouldn't have direct access to a private variable unless it's by using a getter
I don't agree for this. because getter also a public method and no meaning of this sentence.
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
add a comment |
That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea
method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius
.
IMHO, the area
in the class should be calculated, using the radius
, once requested, instead of storing both the radius
and the area
. This is how the program should look:
class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
As you see above, the r vs radius
question goes away completely.
In as far as the current question goes, I agree with you; however, the class should (arguably) overrideObject#equals(Object)
,hashCode()
andtoString()
. There might also be some value to making theradius
immutable and using the constructor to set it.
– Elliott Frisch
Nov 13 at 4:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273499%2fmethod-on-how-to-access-a-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In this case both method call are correct
setArea(Math.PI * radius * radius);
and
setArea(Math.PI*r*r);
public method shouldn't have direct access to a private variable unless it's by using a getter
I don't agree for this. because getter also a public method and no meaning of this sentence.
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
add a comment |
In this case both method call are correct
setArea(Math.PI * radius * radius);
and
setArea(Math.PI*r*r);
public method shouldn't have direct access to a private variable unless it's by using a getter
I don't agree for this. because getter also a public method and no meaning of this sentence.
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
add a comment |
In this case both method call are correct
setArea(Math.PI * radius * radius);
and
setArea(Math.PI*r*r);
public method shouldn't have direct access to a private variable unless it's by using a getter
I don't agree for this. because getter also a public method and no meaning of this sentence.
In this case both method call are correct
setArea(Math.PI * radius * radius);
and
setArea(Math.PI*r*r);
public method shouldn't have direct access to a private variable unless it's by using a getter
I don't agree for this. because getter also a public method and no meaning of this sentence.
answered Nov 13 at 4:00
janith1024
7643916
7643916
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
add a comment |
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
1
1
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
getter is also a public method but have a look at this answer to see why it still is a good practice
– Kartik
Nov 13 at 4:06
add a comment |
That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea
method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius
.
IMHO, the area
in the class should be calculated, using the radius
, once requested, instead of storing both the radius
and the area
. This is how the program should look:
class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
As you see above, the r vs radius
question goes away completely.
In as far as the current question goes, I agree with you; however, the class should (arguably) overrideObject#equals(Object)
,hashCode()
andtoString()
. There might also be some value to making theradius
immutable and using the constructor to set it.
– Elliott Frisch
Nov 13 at 4:50
add a comment |
That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea
method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius
.
IMHO, the area
in the class should be calculated, using the radius
, once requested, instead of storing both the radius
and the area
. This is how the program should look:
class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
As you see above, the r vs radius
question goes away completely.
In as far as the current question goes, I agree with you; however, the class should (arguably) overrideObject#equals(Object)
,hashCode()
andtoString()
. There might also be some value to making theradius
immutable and using the constructor to set it.
– Elliott Frisch
Nov 13 at 4:50
add a comment |
That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea
method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius
.
IMHO, the area
in the class should be calculated, using the radius
, once requested, instead of storing both the radius
and the area
. This is how the program should look:
class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
As you see above, the r vs radius
question goes away completely.
That code feels weird. Usually, when you're calling a set method, or a setter, you are explicitly setting a value, and nothing else. The setArea
method call seems out of place, since that is now a side effect of the stated method call, which only specifies setRadius
.
IMHO, the area
in the class should be calculated, using the radius
, once requested, instead of storing both the radius
and the area
. This is how the program should look:
class Circle {
private double radius;
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
As you see above, the r vs radius
question goes away completely.
answered Nov 13 at 4:08
mjuarez
9,65973751
9,65973751
In as far as the current question goes, I agree with you; however, the class should (arguably) overrideObject#equals(Object)
,hashCode()
andtoString()
. There might also be some value to making theradius
immutable and using the constructor to set it.
– Elliott Frisch
Nov 13 at 4:50
add a comment |
In as far as the current question goes, I agree with you; however, the class should (arguably) overrideObject#equals(Object)
,hashCode()
andtoString()
. There might also be some value to making theradius
immutable and using the constructor to set it.
– Elliott Frisch
Nov 13 at 4:50
In as far as the current question goes, I agree with you; however, the class should (arguably) override
Object#equals(Object)
, hashCode()
and toString()
. There might also be some value to making the radius
immutable and using the constructor to set it.– Elliott Frisch
Nov 13 at 4:50
In as far as the current question goes, I agree with you; however, the class should (arguably) override
Object#equals(Object)
, hashCode()
and toString()
. There might also be some value to making the radius
immutable and using the constructor to set it.– Elliott Frisch
Nov 13 at 4:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273499%2fmethod-on-how-to-access-a-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
It depends on requirement...if you see in Circle class there is <private double radius;> instance variable, they just want to set radius value in that variable as well as wanted to calculate area and again set to instance variable <private double area;>
– Arun Kumar
Nov 13 at 3:54
6
In
setArea
you have a bug; it should bethis.area = a;
notarea = area;
– Elliott Frisch
Nov 13 at 3:55
@ElliottFrisch Apologies, that wasn't in the textbook; I just wrote it quickly for this question. I'll change it.
– Tyler
Nov 13 at 3:56
5
because a public method shouldn't have direct access to a private variable I disagree.
– Elliott Frisch
Nov 13 at 3:58
2
Your classmate is wrong. Public methods access private fields all the time.
– Dawood ibn Kareem
Nov 13 at 3:59