Results 1 to 6 of 6

Thread: What is Instance variables in Java?

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    What is Instance variables in Java?

    Hello Friends,

    I am new in the Java programming with few confusion. I have sound knowledge about the static variable of java, But not getting the instance variables of java. I have referred many java books but didn't get more detail information on instance variables. If you posses any knowledge about the Instance variables in Java, then please let me know that. I am waiting for your reply.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is Instance variables in Java?

    An instance variable of java language is completely opposite to the class variable.instance variable is a special type of the instance member. An example of an instance variable is "private double length". In OOPs with classes, an instance variable is a variable defined in a class, for which each object in the class has a separate copy.Instance Variables (Non-Static Fields) Technically speaking, are objects stored in individual states in "non-static fields", that is, fields declared without the static keyword.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: What is Instance variables in Java?

    All object instances have their own copies of instance variables. Instance variables in java are any variables which is defined within a constructor function and that are copied into each object instance of that constructor. You access instance variables directly from their containing object instances. Suppose there are three object instances of a Circle class then there must be three copies of every instance variable in the class. Because each object instance has its own copy of an instance variable, each object instance can assign a unique value to an instance variable without modifying the values of other copies of the instance variable.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What is Instance variables in Java?

    Hi,

    When a number of objects are created from the same class, they each have their own distinct copies of instance variables. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: What is Instance variables in Java?

    Below java example defines three instance variables and which are mak, model1, color1within a constructor function. These three instance variables are available directly from all object instances of the Cardemo constructor.
    Example:
    Code:
    function Cardemo(mak, model1, color1)
    
     { 
       
    this.mak = mak;
    
    this.model1 = model1;
    
    this.color1 = color1;
    
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is Instance variables in Java?

    Static methods can not access the instance variables in java language, other than via some object reference. While the instance methods can access instance methods as well as the instance variables directly. Static methods may even access private instance variables in their class via some object reference.When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables.

Similar Threads

  1. What are an Atomic Variables in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 04-01-2011, 07:51 PM
  2. About instance variables in java
    By Khaled11 in forum Software Development
    Replies: 1
    Last Post: 19-03-2010, 02:38 PM
  3. How to create an instance of Bean in java?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 09:33 PM
  4. Multiplying Variables in java
    By WinDoWLoCuS in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 01:43 PM
  5. Replies: 5
    Last Post: 02-03-2009, 09:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,533,521.60279 seconds with 17 queries