In programming to hold data members we use variables, Java you can declare three types of variables namely,
In addition to these, they are referred with different names based on the usage.
Fields − Variables of a class i.e. instance variables and static variables are called fields. They can’t be abstract except this you can use any other modifier with fields.
public class Sample{ int data = 90; static data = 145; }
In general, fields with private modifier, setter and getter methods are considered as properties.
public class Sample{ private int name; public String getName(){ return this.number; } public void setName(String name){ this.name = name; } }
public class Student{ private String name; private int age; public Student(String name, int age){ this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } public static void main(String[] args){ Student std = new Student("Krishna", 29); System.out.println(std.getName()); System.out.println(std.getAge()); } }
Krishna 29