Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class and its method

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ultimate aim of Class is ,I should contain Property and method who work on the property.




Is that is the case ,all the details to be passed to the class,should be go through Property only,for this getter and setter is enough ,so the method of the particular class should not take any input parameters and work on the property of the class only?

Ex:

Class A{

String one;
String two;

Void someMethod()
{

// work on the properties one and two
}

//here,Getter and setter for both properties one and two


}



 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your instance variable ONE and TWO will be null initially. getter/setter role is to fetch the current (this) value of these variables. someMethod() may take some input parameter say int i. Inside the method you may write if(i==0) {ONE = "hallo";} // here you have worked on your property based on some input parameter. So, initially calling getter would have fetched null. After calling someMethod(1), getter will fetch hallo.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Getters and Setters are mutators; their only objective is setting and reading the state of an object.

2. Instance methods define or embody behavior/functionality for a particular class. For that they work on the state of object(i.e. properties). In a way, they try to make some meaning of the object state. These methods might need additional data to implement the behavior/functionality. Therefore, sometimes you need to pass them arguments. These arguments are/could be independent of object state and they maybe different for each invocation of the method.

3. Methods that work only with their arguments and independently of the Object state, should be declared static. Utility classes only have static methods with no properties and therefore no mutators.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic