Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: overloaded consctructors

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default overloaded consctructors

    hello,
    for my Java class we need to create an overloaded constructor. I've been watching tutorials on youtube, and the ones I'm watching always seems to have the main method called something, for example, Test, and then a different method ( or constructor - I'm a little hazy on what it's called ) called something else, like Test2.
    So Test would have something like
    class Test
    public static void main(String[] args) {
    Test1 Test1Object = new Test1();

    and the constructor would be something like

    public class Test1{
    public String getName()
    return someonesName;

    So does the constructor need to be a separate file? I'm sorry, I don't know if it's the correct term to call it a file. When I submit my homework, from Eclipse I save the main method as Test, and the constructor as Test1, and they are two separate files, Test.java and Test1.java.
    For this homework though, he's only wanting one file ( called Date ) but the Date method will need several constructors.
    So does it work to have the constructors in the body of the main method?

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: overloaded consctructors

    The constructor for a given class is always the name of the class. Overloading just means that each constructor has different numbers and/or types of arguments. But the name for each overloaded constructor is the same.

    Regards,
    Jim
    Last edited by jim829; October 19th, 2018 at 03:44 PM.

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: overloaded consctructors

    okay, thank you, That helps.
    Unfortunately, from this answer, as I started trying to code this homework assignment, I have another question.
    So, I found out that I'll have a main called Date, and a constructor called DateTest. I have to have a couple different date formats to display, and I found that I think I want to use the java util called
    SimpleDateFormat
    I also think that in my constructors I will have my integers for the date and setup the date formats. And in my main I'll call the different constructors. So wouldn't I need to have the import command
    import java.util.SimpleDateFormat
    in the constructor?
    I wouldn't have the import in the main, is that right?

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: overloaded consctructors

    I'm not certain what you mean by a "main called Date." I will assume you mean you are using the Date class. And your class will be called DateTest (since that is the name of your constructor).

    If you want to use the SimpleDateFormat class you will need to import it or you can just reference it by its full package name (which can get cumbersome).

    Note that it is not necessary to call constructors if you just want to write some code to check out something in the main() method. Unless you need to make use of instance fields or methods defined in your DateTest class you don't really need a constructor.

    Note: SimpleDateFormat is in java.text, not java.util.

    Regards,
    Jim

  5. #5
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: overloaded consctructors

    okay, but I have to have a constructor for my homework assignment. So is it supposed to go in the constructor? Or would it go in the main method.
    Sorry, I'm too new to Java to have a good handle on the terminology. I just mean the method that has
    public static void main(String[] args) {

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: overloaded consctructors

    The constructor is directly inside the class. In most cases the constructor is invoked in main and then used to access other instance methods and fields. Here is an example:


    public class Foo {
         int value;
         public Foo(int v) {
              value = v;
         }
     
         public static void main(String[] args) {
               Foo foo = new Foo(10);
               foo.start();
         }
     
         public void start() {
               System.out.println(value);  // prints 10
         }
    }

    If I wanted to print out "value" from the static main method I would have to qualify it with the foo reference.
    System.out.println(foo.value);

    Regards,
    Jim

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: overloaded consctructors

    I just realized that I didn't really address overloaded constructors. Here is a simple example:

    public class Person {
          String name;
          int age;
     
          public Person(String name, int age) {
                this.name = name;
                this.age = age;
           }
     
           public Person(String name) {
                 // call other constructor with this name and 33
                 this(name, 33);  // default age of 33
           }
     
           public static void main(String args[]) {
                  Person p1 = new Person("Bob", 22);
                  System.out.println(p1);  // should print "Bob 22"
     
                  Person p2 = new Person("Sue");
                  System.out.println(p2);  // should print "Sue 33"
           }
     
           public String toString() {
               return name + " " + age;
           }
    }

    Regards,
    Jim

Similar Threads

  1. PLEASE Help with developing a class with an overloaded method
    By crzyblndgrl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 30th, 2010, 06:24 AM
  2. How overloaded paint() works?
    By maikeru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2009, 06:13 PM