Plzz anyone tell me what is the error in this getter and setter program !! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Plzz anyone tell me what is the error in this getter and setter program !!

https://code.sololearn.com/czqWLDMQJIwD/?ref=app

25th Aug 2017, 7:45 AM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar
3 Answers
+ 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()); } }
25th Aug 2017, 7:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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();
25th Aug 2017, 8:24 AM
Will
+ 3
Sorry Hastei but your program is more complicated I want a simple program with only one class as I'm a beginner.
25th Aug 2017, 10:03 AM
Vaibhav Singh Sikarwar
Vaibhav Singh Sikarwar - avatar