Instance Variable in Java Programming
Java Programming Language / Variables in java
5068
Program:
class InstanceVariable { int data=50;//instance variable public static void main(String []args) { /* In this example if you want to access instanec variable'data' then you have to create an instane of InstancVariable class like this*/ InstanceVariable iv = new InstanceVariable(); /* here iv is the name of the object or instance of the InstanceVariable class */ System.out.println(iv.data); /* to access the instance variable 'data' you have to write iv.data } }
Output:
50 Press any key to continue . . .
Explanation:
Rules for Instance variable in Java
- Instance variables can use any of the four access levels
- They can be marked final
- They can be marked transient
- They cannot be marked abstract
- They cannot be marked synchronized
- They cannot be marked strictfp
- They cannot be marked native
- They cannot be marked static
Cheatsheet
- Public, Private, Protected all 3 access modifiers can be applied to Instance Variable(Default also).
- Instance variable will get default value means instance variable can be used without initializing it. Same is not true for Local Variable.
- Instance Variable can be marked final.
- Instance Variable can be marked transient.
- Instance Variable cannot be abstract.
- Instance Variable can not have synchronized modifier.
- Instance Variable can not have strictfp modifier.
- Instance Variable can not have the native modifier.
- Instance Variable can not have Static modifier as it will become Class level variable.
INSTANCE VARIABLE TYPE | DEFAULT VALUE |
---|---|
boolean | false |
byte | (byte)0 |
short | (short) 0 |
int | 0 |
long | 0L |
char | u0000 |
float | 0.0f |
double | 0.0d |
Object | null |
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS