+ 4
Plzz anyone tell me what is the error in this getter and setter program !!
3 Réponses
+ 9
public class Program {
static class test {
private int number;
public test() {number = 8;}
public int getNumber() {
return this.number;
}
public void setNumber(int c) {
this.number = c;
}
}
public static void main(String[] args) {
test obj = new test();
System.out.println(obj.getNumber());
obj.setNumber(5);
System.out.println(obj.getNumber());
}
}
+ 3
to sum what was answered above, getters and setters used in non-static (dynamic) classes, you would have to create a seprate class and in order to reference a non-static method inside a method class you have to create an instance of the non-static class.
className variableName = new className();
+ 3
Sorry Hastei but your program is more complicated I want a simple program with only one class as I'm a beginner.