0

So I'm a little confused about the concept of

Instance methods of a class being called without first instantiating an object

How does that work in java? Would I have to instantiate an object so I can invoke a method on the object.

For example, here are my classes below.

public class Date{
 public String month;
 public int day;
 public int year;
 public void writeOutput()
 {
 System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
 }
}
public class DateTest{
 public static void main(String[] yolo){
 Date today;
 today = new Date();
 today.month = "January";
 today.day = 31;
 today.year = 2015;
 today.writeOutput();
 }
}

Hence I would have to instantiate some date first? Can I call an instance method without instantiating a "date" object of some kind?

asked Jan 31, 2015 at 20:57
18
  • String a = new Card(); makes no sense. Is it a String or a Card? Please strive for clarity with your questions. Commented Jan 31, 2015 at 20:58
  • And regarding this ""concept": "So I'm a little confused about the concept of "Instance methods of a class being called without first instantiating an object"." -- ignore it as it is not a valid concept. You can only call instance methods on an instance. Period. Commented Jan 31, 2015 at 21:00
  • I think a better way to put it is do you have to instantiate an object before you call an instance method? Commented Jan 31, 2015 at 21:02
  • Assuming you're quoting an error message there then it's analogous to "You tried to post but you don't have a Letter yet (or didn't say which letter you wanted to post)" Commented Jan 31, 2015 at 21:03
  • 1
    Can you make a dog bark if it doesn't exists? (and only the concept of the dogs exists). Now imagine the concept of the dog as the class, bark as a method and Rex as an instance of a dog. So yes, you need to instanciate a class (Dog Rex = new Dog();) In order to use a method (Rex.bark()). Of course you can use static methods that allow you to do something like Dog.bark() but that it's not really OOP. Commented Jan 31, 2015 at 21:05

4 Answers 4

3

This is a copy of my comment:

Can you make a dog bark if it doesn't exists? (and only the concept of the dogs exists). Now imagine the concept of the dog as the class, bark as a method and Rex as an instance of a dog. So yes, you need to instanciate a class (Dog Rex = new Dog();) In order to use a method (Rex.bark()). Of course you can use static methods that allow you to do something like Dog.bark() but that it's not really OOP.

answered Jan 31, 2015 at 21:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That helped a lot. I'm in the process of understanding how classes work, so I might ask a lot of questions if I cannot find the answer in my textbook.
1

Regarding:

Hence I would have to instantiate some date first? Can I call an instance method without instantiating a "date" object of some kind?

Correct. To call an instance method of Date, you must first create a Date object, which you are currently doing in your main method.

 Date today; // declare Date variable today
 today = new Date(); // create instance and assign it to today

Note your code has direct manipulation of Date fields, something that should be avoided.

answered Jan 31, 2015 at 21:15

Comments

1

The statement today = new Date(); instantiates an instance of class Date and assigns a reference to that instance to the variable today.

That allows references to instance variables and methods to be referenced through the today variable. Without an instance, those members wouldn't exist.

answered Jan 31, 2015 at 21:16

1 Comment

Oh, that makes a sense. We can't call date without having the object today to refer to it. I think I'm understanding this better.
0

Yes, you can call a method without instantiating by using a static fields and factory methods.

public class Date{
 public static String month;
 public static int day;
 public static int year;
public static void writeOutput()
{
 System.out.println("Today is : " + this.month + " " + this.day + " , " + this.year);
}}

Then you can do the following:

Date.month = "January";
Date.day = 1;
Date.year=2015;
Date.writeOutput();

That way you don't need to instantiate. However, this is not necessarily good practice. Static factory methods are good if you don't need class variables, but pass the necessary parameters to the method instead. For example:

public class Date
{
public void writeOutput(String year, int day, int month)
{
 System.out.println("Today is : " + month + " " + day + " , " + year);
}
}

Then you can call

Date.writeOutput("January", 1, 1);
answered Jan 31, 2015 at 21:17

2 Comments

I don't think I have learned about static fields and factory methods. I'm not sure about what they mean or refer to.
@Quinty Do not worry about static fields/methods and factory methods as you are starting. Both are violations of the Object-Oriented Programming concepts. They are practical and handy exceptions to the rules. But learn the rules first.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.