Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Why isn't every object of a class bound to assign a value to 'all' instance fields?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Why isn't every object of a class bound to assign a value to 'all' instance fields?

    Conceptually speaking, objects in Java (or any other language) represent/model real-world-objects (which are ). A class is a blueprint for creating objects.

    Then the fields (static and nonstatic) represent the properties of those objects. Static fields represent the properties, whose value is same for all the objects of the class (in other words, a single copy of the field is in existence and every instance can use it), e.g. a property mother's-name would have the same value for three objects which represent three siblings. Non-static fields represent the properties which have different values for different objects (in other words, each object has its own copy of the field), e.g. the property national-identity-card-number for the sibling objects.

    But then, not all objects of a class assign values to all the non-static fields (in their constructors) declared in that class; and that doesn't result in any error.
    Java Code:
    class Sibling {
      String name;
      int age; 
    
      Sibling(String name, int age) {
        this.name=name;
        this.age=age;
      }
    
      Sibling(String name) {
        this.name=name;
      }
    
      public static void main(String[] args) {
        Sibling siblingJ = new Sibling("J", 7);
        Sibling siblingK = new Sibling("K");
    
        System.out.println("name of first sibling: " + siblingJ.name + ", age of first sibling: " + siblingJ.age);
        System.out.println("name of second sibling: " + siblingK.name );
      }
      
    }
    Does this mean that we are modelling a situation where the age of the second sibling does exist, but it is not useful for us for solving the problem at hand, so we are not using it in our program, or the information is not available etc.?

    In other words, is it correct that each instance field declared in a class does exist for every object of that class, but some objects may not assign it a value (and completely forget about its existence) if they don't need it?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Why isn't every object of a class bound to assign a value to 'all' instance field

    It is assigned a value.
    For primitives the defaults are 0 for numeric and false for boolean.
    For objects it is null.

    In the example you give it could simply mean "unknown", though since 0 is a valid age I would look to an alternative way of showing that.

    But there is no such thing as an instance attribute that does not have a value.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Why isn't every object of a class bound to assign a value to 'all' instance field

    Yes, the field exists for every object, regardless if you assigned it a value. They get a default value. Primitives (byte, short, int, double, long) are 0, booleans are false, and objects are null. The object "forgets" nothing, it just not uses it.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why isn't every object of a class bound to assign a value to 'all' instance field

    The 'magic' comes from the construction (not the constructor) of an object: when you say, e.g. 'new X( ... )', memory for a new X object is taken from the heap and the entire memory for that new object is wiped clean, i.e. all its bits are set to zero; for numerical fields this means the value 0 and for object fields this means null. If your constructor or object initialization code doesn't change a field value anymore, it stays 0 or null.

    kind regards,

    Jos
    Zarah likes this.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Why isn't every object of a class bound to assign a value to 'all' instance field

    @Tolls @SurfMan @JosAH Now itt is clear in my mind. Thank you so very much.

Similar Threads

  1. Replies: 4
    Last Post: 12-03-2015, 04:09 PM
  2. Replies: 2
    Last Post: 03-07-2015, 06:24 PM
  3. Replies: 3
    Last Post: 03-24-2010, 01:24 AM
  4. Replies: 2
    Last Post: 03-08-2010, 11:29 AM
  5. Replies: 2
    Last Post: 02-18-2010, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •