» » Instance variables in java with example program

  • Variables declared inside a class and outside method without static keyword known as instance variables.
  • Instance variables will be used by objects to store state of the object.
  • Every object will have their own copy of instance variables. where as static variables will be single and shared(accessed) among objects.
  • Instance variables will be associated with the instance(Object)
  • Static variables will be associated with the class.



#1: Java example program on declaring and accessing instance variables.

  1. package com.instanceofjava.instancevariables;

  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview questions
  5.  * 
  6.  * Description: Instance variables in java with example program
  7.  *
  8.  */
  9. public class InstanceVariables {

  10. String websiteName;
  11. String category;
  12. public static void main(String[] args) {
  13. InstanceVariables obj = new InstanceVariables();
  14. obj.websiteName="www.InstanceOfJava.com";
  15. obj.category="Java tutorial/interview questions";

  16. }

  17. }


  • Instance variables allows all four type of access specifiers in java


instance variables in java with example



  • Instance variables can be final and transient


instance variables in java with example program



  • Instance variables can not be declared as abstract, static strictfp, synchronized and native

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu