Quinty
Quinty

Reputation: 199

Understanding classes in java

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?

Upvotes: 0

Views: 131

Answers (4)

barq
barq

Reputation: 3711

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);

Upvotes: 0

Luis Alves
Luis Alves

Reputation: 1286

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.

Upvotes: 3

TedB
TedB

Reputation: 387

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.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

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.

Upvotes: 1

Related Questions