help im new to this....
why are get method useful?? for example
why write this:

public class name {
	private String yourname;
	
	public void setname(String Fname){
		yourname= Fname;
	}
	public String getname(){
		return yourname;
	}
	public void sayying(){
		System.out.printf("hello  %s", getname());
	}
}

when this also does the same thing

private String yourname;
	
	public void setname(String Fname){
		yourname= Fname;
	}
	public void sayying(){
		System.out.printf("hello  %s", yourname);
	}

Getters and setters are the basics of Object-Oriented Programming, they allow encapsulation of class members. The reason why they are useful is because by making the field private and only allowing that field to be modified through a setter method you can make sure that no one outside of your class changes that field in a wrong way. Taking that a step further you can restrict what kind of values that variable can have. For example:

public class Dog{
    private int legs;

    public void setLegs(int legNum){
        if(legNum > 0 & eyeNum <= 4){ 
            legs = legNum;
        }
    }

    public int getLegs(){
        return legs;
    }
}

Here we have a Dog class, this class has a private field(variable) that holds how many legs the dog has. The reason why you make it private(encapsulating it) is because you wouldn't want any other class being able to use the legs variable however it wishes. You also don't want anyone giving the dog negative legs or 300 legs for the matter. So the bottom line is that the setter and getter methods are meant to be used by other classes so that your variables don't get changed in ways that would cause bugs, otherwise you can use the variable inside of its class however you want(no need to use the gettter method inside of its own class). Hope that clears things up.

Getters and setters are the basics of Object-Oriented Programming, they allow encapsulation of class members.

Then it is not a primary rule for basic bean specification? I have not learned getter and setter methods in object oriented programming.. Now only i am hearing.

Then it is not a primary rule for basic bean specification? I have not learned getter and setter methods in object oriented programming.. Now only i am hearing.

I have never used bean so I wouldn't be able to comment on that, as for getters and setters, that's just one way of calling them, but they have multiple names I call them that because that's how they were taught to me.

Then it is not a primary rule for basic bean specification? I have not learned getter and setter methods in object oriented programming.. Now only i am hearing.

In Spring (beans) the primary purpose of a bean is to abstract you away from the class and encapsulate the code and allow it to be populated via another means. You can load your class variables up using a spring bean file. The underlying code is usually a class of basic getters/setters with some implementation.

As for getters/setters

Getters and setters allow you to wrap data elements with meaningful code that the callers of said method would never need to know.

For Example

public class foo { 
  public int width;
  public int height;
  public Image image;

  public foo() { } 

}

By exposing those elements directly your user has no inclining into the manner in which they should be connected together. Is that width and height for something else, is that width and height of the image?

By exposing your code via getters and setters you can write impl code that will allow callers to use the methods without worry of the underlying happenings of the class.

public class foo { 
  private int width;
  private int height;
  private Image image;

  public foo() { } 

  public void setImage(Image i) { 
    image = i;
    width = image.getWidth(null);
    height = image.getHeight(null);
  }
  public int getWidth() { 
    return width;
  }
  public int getHeight() {
    return height;
  }
  // Here we can see that you can expose getters for elements that don't have variables assigned to them - think of this as reducing code on the callers end
  public int getArea() { 
    // Users of your code don't need to know how this happens, just that they can use getArea() and get the area
    return (getWidth() * getHeight());
  }
}
Member Avatar for hfx642

You CAN call the methods whatever you want, but...
getWhatever() and setWhatever() are pretty self expanatory.

help im new to this....
why are get method useful?? for example
why write this:

public class name {
	private String yourname;
	
	public void setname(String Fname){
		yourname= Fname;
	}
	public String getname(){
		return yourname;
	}
	public void sayying(){
		System.out.printf("hello  %s", getname());
	}
}

when this also does the same thing

private String yourname;
	
	public void setname(String Fname){
		yourname= Fname;
	}
	public void sayying(){
		System.out.printf("hello  %s", yourname);
	}

yes, in this example it does the same .. but say that you are writing an application that has to use, say an array of Student objects (name, age, class) and create for each of those that are enrolled in the class "Java", a printout in another class.

the approach with just printing the value wouldn't do you any good.
you would have to return for each Student object in your array the value of the variable 'class', compare it to the String "Java", and, if it is equal, print a report, otherwise, go to the next Student object.

Off course, you could put that logic within your Student class, in the getClass method, for instance by returning true if the value equals Java, but what are you going to do if you need to write the same application for those students who take the Math class instead of Java?

That would mean, you would either re-write (or extend) your Student class, or, add yet another method to check if they're enrolled in Math. Since it's not the developers job to know all the classes on earth (not even possible, IMHO) you would limit your class for your use only.

also, putting a print like that in your class is a very bad idea. Yes, it will work as long as it is an application which is run through command prompt, but how is the user going to get the output if that particular bean is used within a web application?

commented: good suggestios +1 +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.