Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree2Likes

Thread: Using a Constructor in Java

  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Using a Constructor in Java

    How do you set up/use a constructor in java?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Come on now -- the basic tutorials will surely show you how to do this. Rather than have us re-write one of these, why don't you tell us exactly what confuses you.

  3. #3
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    I have read this article repeatedly and I think what it says the following would be true:
    Java Code:
    //This is creating the constructor and initiliazing the variables, Make, Model and Trans
    public Car()
    {
    Make = Ford;
    Model = Mustang;
    Trans = Manual;
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Quote Originally Posted by Rahim2312 View Post
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    I have read this article repeatedly and I think what it says the following would be true:
    Java Code:
    //This is creating the constructor and initiliazing the variables, Make, Model and Trans
    public Car()
    {
    Make = Ford;
    Model = Mustang;
    Trans = Manual;
    }
    What errors are you getting when you try this?

    Also, do you notice the big difference between your code attempt and the first example in the link you've provided?

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    I haven't begun writing the code yet, I was trying to wrap my mind around the constructor piece before I began.

    The difference that I see between my sample code, and the code I was trying to copy from is that my code does not receive any parameters, whereas the code from the site receives 3 parameters?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Quote Originally Posted by Rahim2312 View Post
    I haven't begun writing the code yet, I was trying to wrap my mind around the constructor piece before I began.
    Better to write the code before you come here. We're no substitute for your compiler, and you really can't break the compiler or computer with experimentation. Yes you'll get errors, but by trying to understand the errors and working with the code, you'll gain a much deeper appreciation for compilers than if we spoon-fed you an answer.


    The difference that I see between my sample code, and the code I was trying to copy from is that my code does not receive any parameters, whereas the code from the site receives 3 parameters?
    Correct.

    Happy coding.

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Using a Constructor in Java

    Constructor is a special method that allows you to create an instance of a class. Just like a method you can declare a constructor to accept parameters. The only different is that a constructor does not have a return type and it must have a name that match the class name.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: Using a Constructor in Java

    Quote Originally Posted by wsaryada View Post
    Constructor is a special method ...
    A constructor is not a method.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Using a Constructor in Java

    Thanks for the correction DB :)

  10. #10
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    So I have spent about the past 8 hours reading on Constructors and examples and trying to wrap my mind around it. I think I am finally getting it. Expanding on my Car example, I horribly coded wrong yesterday evening...Does this coding look correct to the master coders here?
    Java Code:
    Public Class Car
    {
    	String make;
    	String model;
    	String trans;
    	
    	public int getInfo()
    	{
    		return make + model + trans;
    	}
    	
    	Car()
    	{
    		make = Ford;
    		model = Mustang;
    		trans = Manual;
    	}
    	
    	Car(string m, string, mo, string t)
    	{
    		make = m;
    		model = mo;
    		trans = t;
    	}
    	
    	Public Static void main (String[] args)
    	{
    		Car carObj1, carObj2;
    		carObj1 = new Car();
    		carObj2 = new Car(Ford, Mustang, Manual);
    		System.out.println("The Make, Model and Transmission of the car is: " + carObj1.getInfo());
    	}
    }

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: Using a Constructor in Java

    Does it compile?
    Does it run?
    Does it perform as expected?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    Guess I am not understanding it as I thought I was. No it will not compile...below are my errors:
    C:\Programs>javac test.java
    test.java:1: error: class, interface, or enum expected
    Public Class Car
    ^
    test.java:4: error: class, interface, or enum expected
    String model;
    ^
    test.java:5: error: class, interface, or enum expected
    String trans;
    ^
    test.java:7: error: class, interface, or enum expected
    public int getInfo()
    ^
    test.java:10: error: class, interface, or enum expected
    }
    ^
    test.java:15: error: class, interface, or enum expected
    model = Mustang;
    ^
    test.java:16: error: class, interface, or enum expected
    trans = Manual;
    ^
    test.java:17: error: class, interface, or enum expected
    }
    ^
    test.java:22: error: class, interface, or enum expected
    model = mo;
    ^
    test.java:23: error: class, interface, or enum expected
    trans = t;
    ^
    test.java:24: error: class, interface, or enum expected
    }
    ^
    test.java:29: error: class, interface, or enum expected
    carObj1 = new Car();
    ^
    test.java:30: error: class, interface, or enum expected
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    test.java:31: error: class, interface, or enum expected
    System.out.println("The Make, Model and Transmission of the car is: " +
    carObj1.getInfo());
    ^
    test.java:32: error: class, interface, or enum expected
    }
    ^
    15 errors

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Your class name must match the name of the file. Thus if you've got a class name of "Car" then the file name must be "Car.java".

  14. #14
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Using a Constructor in Java

    And remember that Java is case sensitive language, so lowercase and uppercase is a different thing. For instance when you declare your class you are using a wrong keyword, Public vs public.
    Fubarable likes this.

  15. #15
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    I knew the file name had to be the same name...I had just forgotten that. This has been updated so the file is named Car.java and the class is named Car.

    I did not know that java was case sensitive! Thank you for pointing that out.

    Now with that being said and both of those obvious issues being fixed, I am still getting the same compile errors as I did before (post 12)

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    If you're still having problems, even if they seem the same, please re-post your code with code tags and the full list of errors.

  17. #17
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    Code:
    Java Code:
    Public Class Car
    {
        String make;
        String model;
        String trans;
         
        public int getInfo()
        {
            return make + model + trans;
        }
         
        Car()
        {
            make = Ford;
            model = Mustang;
            trans = Manual;
        }
         
        Car(string m, string, mo, string t)
        {
            make = m;
            model = mo;
            trans = t;
        }
         
        public static void main (String[] args)
        {
            Car carObj1, carObj2;
            carObj1 = new Car();
            carObj2 = new Car(Ford, Mustang, Manual);
            System.out.println("The Make, Model and Transmission of the car is: " + carObj1.getInfo());
        }
    }
    List of errors:
    c:\test>javac car.java
    car.java:1: error: class, interface, or enum expected
    Public Class Car
    ^
    car.java:4: error: class, interface, or enum expected
    String model;
    ^
    car.java:5: error: class, interface, or enum expected
    String trans;
    ^
    car.java:7: error: class, interface, or enum expected
    public int getInfo()
    ^
    car.java:10: error: class, interface, or enum expected
    }
    ^
    car.java:15: error: class, interface, or enum expected
    model = Mustang;
    ^
    car.java:16: error: class, interface, or enum expected
    trans = Manual;
    ^
    car.java:17: error: class, interface, or enum expected
    }
    ^
    car.java:22: error: class, interface, or enum expected
    model = mo;
    ^
    car.java:23: error: class, interface, or enum expected
    trans = t;
    ^
    car.java:24: error: class, interface, or enum expected
    }
    ^
    car.java:26: error: class, interface, or enum expected
    public static void main (String[] args)
    ^
    car.java:29: error: class, interface, or enum expected
    carObj1 = new Car();
    ^
    car.java:30: error: class, interface, or enum expected
    carObj2 = new Car(Ford, Mustang, Manual);
    ^
    car.java:31: error: class, interface, or enum expected
    System.out.println("The Make, Model and Transmission of the car is: " +
    carObj1.getInfo());
    ^
    car.java:32: error: class, interface, or enum expected
    }
    ^
    16 errors

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    This error:

    c:\test>javac car.java

    suggests that either your file is named car.java rather than Car.java or that you're typing in car.java when you should be typing in Car. java when calling javac.exe. Remember that capitalization matters.

  19. #19
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Using a Constructor in Java

    The name of my file is car.java (I just double checked to make sure :) )

    other files that I want to open, I always type javac filename.java to create my class file, then type java filename to execute the program.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Using a Constructor in Java

    Quote Originally Posted by Rahim2312 View Post
    The name of my file is car.java (I just double checked to make sure :) )
    Good, so then we know how to fix it: name it so that it now is Car.java. This is very important.

    Than call javac Car.java

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  2. Java Constructor Question
    By ashly in forum New To Java
    Replies: 6
    Last Post: 03-09-2010, 02:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Help with constructor in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 01:00 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
  •