Results 1 to 10 of 10

Thread: Constructor

  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default Constructor

    Java Code:
    class A
    {
    	int a=10;
    	int b;
    	int c;
    	A(int i)
    	{
    		b=i;
    	}
    }
    public class C_onstructor
    {
    	public static void main(String args[])
    	{
    		A ob=new A(20);
    		System.out.println("a "+ob.a);//result=10
    		System.out.println("b "+ob.b);//result=20
    		System.out.println("c "+ob.c);//result=0
    		ob.c=30;
    		System.out.println("a "+ob.a);//result=10
    		System.out.println("b "+ob.b);//result=20
    		System.out.println("c "+ob.c);//result=30
    	}
    }
    Hello
    1)I created a class with 3 filed. I initialized a with value 10 at the point of definition.
    I will initialize b with Constructor. what is the difference between two approaches?

    2)Before i used from ob.c=30;,value of c was 0. Did A(int i) cause that c=0?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    1) The difference occures due to a lack of encapsulation. Normally those fields are/should be private. As you have only provided a parameterized constructor, you can only construct objects with field b set by the caller.

    2) c is an int, a primitive that has a default value of 0.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    We know that
    It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    And we know that A ob = new A(); call default constructor to initialize fields by the compiler.

    that is, fields initialize two times?
    Java Code:
    class A
    {
    	private int a=10;
    	private int b;
    	private int c;
    	
    	void get()
    	{
    		System.out.println("a "+a);//result=10
    		System.out.println("b "+b);//result=0
    		System.out.println("c "+c);//result=0
    	}
    
    
    }
    public class C_onstructor
    {
    	public static void main(String args[])
    	{
    		A ob = new A();
    		ob.get();
    	}
    }
    Now I define my constructor,then default constructor is not called. in this case that c becomes zero, then c has default value.
    Java Code:
    class A
    {
    	private int a=10;
    	private int b;
    	private int c;
    	
    	A(int b)
    	{
    		this.b = b;
    	}
    	void get()
    	{
    		System.out.println("a "+a);//result=10
    		System.out.println("b "+b);//result=20
    		System.out.println("c "+c);//result=0
    	}
    
    
    }
    public class C_onstructor
    {
    	public static void main(String args[])
    	{
    		A ob = new A(20);
    		ob.get();
    	}
    }
    I am actually confused and mixed!

  4. #4
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi,
    Hope you are at good health.I am not getting what you want to ask but lets i try to explain

    Actually in your first code you declare c a primitive type and also give default modifier(when no modifier is present then it considered default means only classes with in the same package can access it). All primitive variables by default get 0 value.When you made an instance of class A you assign value to b only

    [code]
    A(int i)
    {
    b=i;
    }

    A ob=new A(20);


    [\code]
    that's why when first time you try to get value of c by ob.c you get 0 i.e., it's default value. Then you assign value to c by ob.c because it has a default modifier it's fine because both are in same package( i think both are in default package) and second time you got 30.

    And the same is happening second time you didn't assign any value to c so it's default value is 0

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    As basit indicated.

    1st code
    has default constructor with no parementers. where the b and c value are set to zero. and a has a default value to 10.

    2nd code
    Has a constructor with int parametner. Where a and b value are set to a specific value and c has a default value to zero.

    So that explains the println values

  6. #6
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I thank you about your attention and your answering.
    I am well, but i do not understand on thing about behavio(u)r of compiler.
    first: when i define a class with some fields and i do not define any constructor, then:
    a)fields get value 0 as defualt?
    b) or defualt constructor gives value 0 to fields?
    if defualt constructor does not do this work, why compiler does invoke it when a class create with new?

  7. #7
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    I do not joke at all. Actually i understand. In the first aspect, it seems simply, but after it becomes hardly.
    I do not know, maybe this subject is mixed for me. I do not know, I do not know...

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I found a new text about creation an object.
    To summarize the process of creating an object, consider a class called Dog:

    1-Even though is does not explicity use the static keyword, the constructor is actually a static method. So the first time an object of type Dog is create, or the first time a static method or static field of
    class Dog is accessed, the Java iterpreter must locate Dog.class, which it does by searching through the classpath.
    2-As Dog.class is lodded, all of its static initializers are run. Thus, static initializatioin takes place only once, as the class object is lodded for the first time.
    3-When you create a new Dog(), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
    4-This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values(zero for numbers and the eqivalent for boolean and char) and the references to null.
    5-Any initializations that occur at the point of field definition are executed.
    6-Constructors are executed. This might actually involve a fair amount of activity, especially when injeritance is involved.

    From: Bruce[1].Eckel.Thinking.In.Java.4th.Edition.Dec.2007.eBook-BBL

  9. #9
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi,
    In the first code when you defined your won constructor then the default constructor won't called. So in the first code when you declared your own constructor then all the instance variables set by their default values.First you set the value of A i.e. 10 so when constructor is created 10 is assign to A, also while creating constructor you assigned value to B so B is also set by its value but c not given any value by that time so c is zero.Now you have created your constructor so now you have the ability to set values of instance variable using notation object.value and then you set the c's value ok.

    When you define no constructor then default constructor is called, as soon default constructor is called all values assign to instance variable so in case of no constructor you only set A = 10 and B and C only declared (int B, int C) not initialized(int A=10) so when default constructor is called then A is set to 10 but B and C are 0(default values)
    Hope now you understand the behavior of constructor

    Thanks

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Bruce isn't entirely correct in that summary.
    5 occurs at a point in 6, just after the implicit (or explicit) call to super() by the constructor.
    I think it was Jos had an example that showed that in action.

Similar Threads

  1. Method and Constructor
    By swikar.java in forum New To Java
    Replies: 7
    Last Post: 10-21-2008, 02:41 PM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 AM

Posting Permissions

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