• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java method get and set  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo there, I still not yet understand about method "set&get"?
Ex/kub.class




public class kub{
public int side=0;

public kub(int side){
this.side = side;
}

//this problem
//if I change this 2 method get&set to comentar.../ nothing make!
//this problem 1# method set
public void setSide(int side){
this.side = side;
}
//this problem 2# get
public in getSide(){
return this.side;
}
//end

public int Area(){
return (12*this.side);
}
public String toString(){
String ag=
"Cube" "\n"
"Side " "\n"
"Area " "Area() "\n";
return ag;
}
public static void main(String[] arg){
kub a = new kub(10);
System.out.println(a);
}




ask?
If I run that code output
Cube
Side 10
Area 120
but if I comentar that 2 method get&set, its output still same. So what its mean / funtion that 2 method?...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

In future please use the code button and indent your code; the post is very difficult to read. But it is possible to see that code will never compile; I think you need some + operators in the toString method.
Never make variables public. Always make them private.
There is nothing wrong with the getSide method. The Area method should be called getArea and that would be all right but you have calculated area wrongly.
I would suggest that setXXX methods are dubious. Do you want the state of your object changing? Remember you should only have a setSide method if you are going to have setXXX methods at all. You have one field, side, and you calculate area and volume from the side.
 
Firman creed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, your welcome sir & thank you has replied.
So, for 'toString()' I forget to writing + operator di post. Than about getXXX&setXXX .
Sir for setSide() mine that same? With constructor?
Like that you said "setXXX() only sate object change". Than like example above;
public class Person{
public String name;

public Person(String newName){
name = newName;
}

public void setName(String newName){
name = newName;
}


public String geName(){
return name;
}

public String toString(){
String ab="Name "+name;
return ab;
}
public static void main(String arg[]){
Person p = new Person("Bambang");
System.out.println(p.getName());
System.out.pintln(p);
}

}

he adds setXXX() like you said.so what's mean from setName() from that code. Otherwise he calls '(p.getName());' at last no p.setName(). What's influence setXXX() sir.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, those are normal setName and getName methods.
 
Firman creed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Normal'.my mean he was maked:
that's code almost same with constructor
....void setName(String newName) not return value,so why he writes setName(String newName) what for?
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo Firman !

Your Kub class had a working set & get methods, but the toString() method was wrong. I have refactored and corrected it:

 
Firman creed
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo Lucian Whiteman!
I forggoten add + operator.
Ok,so I wanna ask it's here is using "public void setSide(int side)". It's type "void" not return value, so why must writing/using, if I not use this method,whether no problem?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
side should be private.
 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my view op is trying to ask if we can intialize instance variable by constructor then why use setter method

Ans-I think if you want to make (private)instance variable accessible from outside class but not modifiable from outside class
Then you can make setter private and getter public
You don't want to make constructor private because then you won't be able to extend your class or create its object outside class


Setter method has several advantages it checks before assigning value.This facility is not provided when you directly access it
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!