Results 1 to 10 of 10
Like Tree8Likes
  • 1 Post By Norm
  • 1 Post By asdfg
  • 1 Post By Bala Eesan
  • 1 Post By cronnin
  • 1 Post By Bala Eesan
  • 2 Post By jim829
  • 1 Post By Tolls

Thread: Is my Constructor is wrong?

  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Is my Constructor is wrong?

    Java Code:
    class Cons
    {
        int l,b,h;
    	public Cons()
    	{
    	   l=10;b=20;h=30;
    	}
        public static void main(String Dev[])
    	{
    	    Cons c=new Cons();
    		System.out.println(b);
    	}
    
    }
    i make a constructor but now i want to print these values but can't...

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Is my Constructor is wrong?

    What happens when you compile the code?
    If there are error messages, copy the full text and paste it here.
    vknehra10 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: Is my Constructor is wrong?

    In the main method, you have not used object, that you have just created.
    constructor is good.
    Last edited by asdfg; 01-22-2017 at 07:33 PM.
    vknehra10 likes this.

  4. #4
    Bala Eesan is offline Member
    Join Date
    Jan 2017
    Posts
    5
    Rep Power
    0

    Default Re: Is my Constructor is wrong?

    Your code is perfectly fine except that the integer variables l, b and h should be declared as static. Only static variables can be accessed within a static method (main).
    Therefore the 3rd line should be coded as shown below.
    static int l,b,h;

    Alternatively, you can call a non static variable within a static method using its object (which is "c" in your code). Hence modifying the 11th line as shown below will also work.
    System.out.println(c.b);

    -- All the best!
    vknehra10 likes this.

  5. #5
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: Is my Constructor is wrong?

    And watch for line 13 misguiding syntax:

    Java Code:
    class Cons
    {
        static int l=11,b=22,h=33;
        public Cons()
        {
           l=10;b=20;h=30;
        }
        public static void main(String Dev[])
        {
            System.out.println("Class variable b is equal to: " + Cons.b);
            
            Cons c=new Cons();
            System.out.println("Some variable b is equal to: " + c.b);
            System.out.println("Some variable b is equal to: " + Cons.b);
        }
     
    }
    Last edited by cronnin; 01-28-2017 at 05:49 PM.
    vknehra10 likes this.

  6. #6
    Bala Eesan is offline Member
    Join Date
    Jan 2017
    Posts
    5
    Rep Power
    0

    Default Re: Is my Constructor is wrong?

    Yup..making a variable or a method to be static allows direct access through its class name with no need for an object.
    vknehra10 likes this.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is my Constructor is wrong?

    I recommend staying away from static fields. They should be used sparingly and only for things which are common across all classes. The best thing to do
    is simply get out of your static context and access your instance fields normally. Static methods should only be used if no instance fields are involved (see Math class
    for examples). Otherwise, use instance methods.

    Regards,
    Jim
    Tolls and vknehra10 like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Is my Constructor is wrong?

    As jim says, making something static just because you can is not really a good reason.
    Java is an OO language, so you should be thinking in terms of objects anyway.
    vknehra10 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Zypher_136 is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default Re: Is my Constructor is wrong?

    Your constructor is not wrong. When you do System.out.println(c) it prints out a "string representation" of your object. However you need to tell java how do you want your objects to be represented by defining the toString() method. By default this method returns the memory location of your object but you dont want that. You need to override the toString() method so when you do System.out.println(c) it actually prints l, b and h. Look at the example below. Hope that helps

    Java Code:
    public class Cons {
    	
    	int l;
    	int b;
    	int h;
    	
    	public Cons() {
    		l = 10;
    		b = 20;
    		h = 30;
    	}
    	
    	public String toString() {
    		return ("l is equal to: " + l
    				+ "b is equal to: " + b
    				+ "h is equal to: " + h);
    	}
    	
    	public static void main(String[] args) {
    		
    		Cons c = new Cons();
    		System.out.println(c);
    	}
    
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Is my Constructor is wrong?

    Consider the following alternative.

    Java Code:
    int l;    int b;
    int h;
         
    public Cons() {
       this(10,20,30); // default values
    }
    
    public Cons(int l, int b, int h) {
        this.l = l;
        this.b = b;
        this.h = h;
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. What's wrong with my constructor?
    By SamJava_the_Hut in forum New To Java
    Replies: 29
    Last Post: 09-24-2013, 06:48 PM
  2. Replies: 10
    Last Post: 01-11-2013, 08:16 PM
  3. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  4. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  5. Replies: 5
    Last Post: 12-14-2011, 01:47 PM

Tags for this Thread

Posting Permissions

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