12 Replies - 2168 Views - Last Post: 19 August 2013 - 10:05 AM Rate Topic: -----

#1 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Constructors

Posted 18 August 2013 - 08:28 PM

Is it a requirement in every Java program that you should always initialize your instance variables in a constructor? And what are the use of set methods? I have this code.

public class Date
{
  private int month;
  private int day;
  private int year;
  
  public Date(int mm, int dd, int yyyy)
  {
    month = mm;
    day = dd;
    year = yyyy;
  }
  
  public void setMonth(int mm)
  {
    month = mm;
  }
  public void setDay(int dd)
  {
    day = dd;
  }
  public void setYear(int yyyy)
  {
    year = yyyy;
  }
  public int getMonth()
  {
    return month;
  }
  public int getDay()
  {
    return day;
  }
  public int getYear()
  {
    return year;
  }
  public void displayDate()
  {
    System.out.println(month+"/"+day+"/"+year);
  }
}




And this is the class which has the main method.

import java.util.Scanner;

public class DateTest
{
  public static void main(String[] args)
  {
    Date dt = new Date(0,0,0);
    
    Scanner input = new Scanner(System.in);
    int month,day,year;
    
    System.out.println("Enter the month: ");
    month = input.nextInt();
    dt.setMonth(month);
    System.out.println("Enter the day: ");
    day = input.nextInt();
    dt.setDay(day);
    System.out.println("Enter the year: ");
    year = input.nextInt();
    dt.setYear(year);
    
    dt.displayDate();
  }
}



Please correct me if I'm wrong or if you have any suggestions. I'm still practicing OOP coding style. Thanks! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Constructors

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,575
  • Joined: 29-July 11

Re: Constructors

Posted 18 August 2013 - 09:01 PM

It's not a requirement, no, but in your case you have to because the instance variable values are passed to you via the constructor.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Constructors

Posted 18 August 2013 - 10:20 PM

it is not a requirement. Instance variable are initialized to:
- 0 for byte, int, char, short, long
- false for boolean
- 0.0 fort double and float
- null for Object

This post has been edited by pbl: 18 August 2013 - 10:22 PM

Was This Post Helpful? 2
  • +
  • -

#4 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: Constructors

Posted 18 August 2013 - 10:41 PM

View Postpbl, on 19 August 2013 - 05:20 AM, said:

it is not a requirement. Instance variable are initialized to:
- 0 for byte, int, char, short, long
- false for boolean
- 0.0 fort double and float
- null for Object


Oh. I get it now. I will just initialized an instance variable via constructor if I don't want its value to be default. For instance, I would like my instance variable type double to have an initial value of 50.0. Does it mean that I'm not allowed to do these private double myDouble = 50.0; ?
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Constructors

Posted 18 August 2013 - 11:22 PM

Instance variables if not initialized are initialized by the compiler to the values that pbl suggested.
This does not mean that you cannot initialize a variable to a default value like so: int month = 10;
Thus if the user does not edit the month variable in anyway and proceeds to print it or work with it, the data that he is working with will be the initialized value that you set. So this would print "The month is: 10" in the code below:


public class Date
{
  private int month;
  private int day;
  private int year;
  
  Date () { }//blank constructor


  public Date(int mm, int dd, int yyyy)
  {
    month = mm;
    day = dd;
    year = yyyy;
  }
  
  public void setMonth(int mm)
  {
    month = mm;
  }
  public void setDay(int dd)
  {
    day = dd;
  }
  public void setYear(int yyyy)
  {
    year = yyyy;
  }
  public int getMonth()
  {
    return month;
  }
  public int getDay()
  {
    return day;
  }
  public int getYear()
  {
    return year;
  }
  public void displayDate()
  {
    System.out.println(month+"/"+day+"/"+year);
  }
}


//in your main class
import java.util.Scanner;

public class DateTest
{
  public static void main(String[] args)
  {
    Date dt = new Date(); 
   /* note the blank constructor in your class, if you create a paramaterized constructor then you must create a blank constructor as the        compiler no longer does this for you as it assumes you will if need be (essentially) */
   System.out.println("The month is: " + dt.getMonth());
    
   
  }
}


It is better practice however to initialize your data by using the constructor as you did above. Also note that you do not need to declare your constructors public as it is inherently assumed they are anyway.

EDIT: Please note that you MUST initialize local variables. This is not the case with instance variables however.

This post has been edited by v0rtex: 19 August 2013 - 11:33 AM

Was This Post Helpful? 1
  • +
  • -

#6 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Constructors

Posted 18 August 2013 - 11:35 PM

Be careful naming classes the same as a class in the Java library, Date. Also I would reconsider using setter methods and just create the object after finding the values, i.e.

    private DatesDemo() {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the month:");
        month = scanner.nextInt();        
        
        System.out.println("Enter the day:");
        day = scanner.nextInt();        
        
        System.out.println("Enter the year:");
        year = scanner.nextInt();        
        
        Dates dates = new Dates(month, day, year);
        System.out.println(dates.toString());
    }


Was This Post Helpful? 2
  • +
  • -

#7 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: Constructors

Posted 19 August 2013 - 12:47 AM

View Postv0rtex, on 19 August 2013 - 06:22 AM, said:

Instance variables if not initialized are initialized by the compiler to the values that pbl suggested.
This does not mean that you cannot initialize a variable to a default value like so: int month = 10;
Thus if the user does not edit the month variable in anyway and proceeds to print it or work with it, the data that he is working with will be the initialized value that you set. So this would print "The month is: 10" in the code below:


public class Date
{
  private int month;
  private int day;
  private int year;
  
  Date () { }//blank constructor


  public Date(int mm, int dd, int yyyy)
  {
    month = mm;
    day = dd;
    year = yyyy;
  }
  
  public void setMonth(int mm)
  {
    month = mm;
  }
  public void setDay(int dd)
  {
    day = dd;
  }
  public void setYear(int yyyy)
  {
    year = yyyy;
  }
  public int getMonth()
  {
    return month;
  }
  public int getDay()
  {
    return day;
  }
  public int getYear()
  {
    return year;
  }
  public void displayDate()
  {
    System.out.println(month+"/"+day+"/"+year);
  }
}


//in your main class
import java.util.Scanner;

public class DateTest
{
  public static void main(String[] args)
  {
    Date dt = new Date(); 
   /* note the blank constructor in your class, if you create a paramaterized constructor then you must create a blank constructor as the        compiler no longer does this for you as it assumes you will if need be (essentially) */
   System.out.println("The month is: " + dt.getMonth());
    
   
  }
}


It is better practice however to initialize your data by using the constructor as you did above. Also note that you do not need to declare your constructors public as it is inherently assumed they are anyway.


Got it! Thanks for your reply Sir. :)
Was This Post Helpful? 0
  • +
  • -

#8 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: Constructors

Posted 19 August 2013 - 12:56 AM

View Postschutzzz, on 19 August 2013 - 06:35 AM, said:

Be careful naming classes the same as a class in the Java library, Date. Also I would reconsider using setter methods and just create the object after finding the values, i.e.

    private DatesDemo() {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the month:");
        month = scanner.nextInt();        
        
        System.out.println("Enter the day:");
        day = scanner.nextInt();        
        
        System.out.println("Enter the year:");
        year = scanner.nextInt();        
        
        Dates dates = new Dates(month, day, year);
        System.out.println(dates.toString());
    }



Thanks for the tip sir. I removed the set methods and I only used the constructor. What I don't get is that dates.toString() because the output is not right. For instance, I entered 09,17,1994. This will be the output: Dates@1eda401
Was This Post Helpful? 0
  • +
  • -

#9 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Constructors

Posted 19 August 2013 - 01:44 AM

Ah, my apologies. I overrided the toString method in my testing of this. Instead of using a void to print your dates.

    @Override
    public String toString() {
        return month+"/"+day+"/"+year;
    }


Was This Post Helpful? 1
  • +
  • -

#10 ChrisNt  Icon User is offline

  • cute bug

Reputation: 212
  • View blog
  • Posts: 648
  • Joined: 31-July 13

Re: Constructors

Posted 19 August 2013 - 01:46 AM

Method toString() returns a representation ( Dates@1eda401 ) of your object,but the default implementation of this method doesnt provide you much information so it is recommended to override it to return an informative representation that is easy for a person to read.


public String toString() {
       return ...readable representation of your object....;
}


This post has been edited by ChrisNt: 19 August 2013 - 01:49 AM

Was This Post Helpful? 2
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Constructors

Posted 19 August 2013 - 06:30 AM

Like this:
http://www.dreaminco...cs-of-tostring/
Was This Post Helpful? 1
  • +
  • -

#12 DaMi25  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-December 12

Re: Constructors

Posted 19 August 2013 - 08:26 AM

View Postschutzzz, on 19 August 2013 - 08:44 AM, said:

Ah, my apologies. I overrided the toString method in my testing of this. Instead of using a void to print your dates.

    @Override
    public String toString() {
        return month+"/"+day+"/"+year;
    }



Thank you sir. "override" from the word itself, in this case, there is a toString method which has different implementation and you created a method which has the same name, in short, your method will be called and its implementation will be executed? Just an idea. :)

View PostChrisNt, on 19 August 2013 - 08:46 AM, said:

Method toString() returns a representation ( Dates@1eda401 ) of your object,but the default implementation of this method doesnt provide you much information so it is recommended to override it to return an informative representation that is easy for a person to read.


public String toString() {
       return ...readable representation of your object....;
}



Got it. Thanks for the explanation. :)

View PostDogstopper, on 19 August 2013 - 01:30 PM, said:



Thank you! I'll surely read this. Very good tutorial. :)
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Constructors

Posted 19 August 2013 - 10:05 AM

View PostDaMi25, on 19 August 2013 - 10:26 AM, said:

View Postschutzzz, on 19 August 2013 - 08:44 AM, said:

Ah, my apologies. I overrided the toString method in my testing of this. Instead of using a void to print your dates.

    @Override
    public String toString() {
        return month+"/"+day+"/"+year;
    }



Thank you sir. "override" from the word itself, in this case, there is a toString method which has different implementation and you created a method which has the same name, in short, your method will be called and its implementation will be executed? Just an idea. :)/>


Yes. This is one of the most important parts of object oriented programming. Overriding allows you to change the default behavior of something even when calling through a superclass. In this case, toString() is defined in the Object class, so every object has it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1