- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a variable, field, property in Java?
In programming to hold data members we use variables, Java you can declare three types of variables namely,
- Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
- Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
- Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.
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.
Example
public class Sample{ int data = 90; static data = 145; }
Property
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; } }
Example
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()); } }
Output
Krishna 29
- Related Articles
- What is a final variable in java?
- What is variable shadowing in java?
- What is a blank uninitialized final variable in java?
- What is Variable Handle in Java 9?
- What is JAVA_HOME variable in Java Environment?
- What is instance variable hiding in Java?
- What is static blank final variable in Java?
- What is blank or uninitialized final variable in Java?
- What is a variable?
- What is the default value of a local variable in Java?\n
- What is date and time temporal field in Java?
- What are variable arguments in java?
- Can a Java array be declared as a static field, local variable or a method parameter?
- What is a reference variable in C++?
- What is a crop field?

Advertisements