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?

Quinty
  • 189
  • 1
  • 1
  • 12
  • `String a = new Card();` makes no sense. Is it a String or a Card? Please strive for clarity with your questions. – Hovercraft Full Of Eels Jan 31 '15 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. – Hovercraft Full Of Eels Jan 31 '15 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? – Quinty Jan 31 '15 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)" – Richard Tingle Jan 31 '15 at 21:03
  • Yes, that is correct. But again, your code doesn't make sense. Why call a Card constructor and assign it to String?? Please fix that so I can remove a down-vote. – Hovercraft Full Of Eels Jan 31 '15 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. – Luis Alves Jan 31 '15 at 21:05
  • I'm a bit confused still. When you call an instance method, does there have to be an object at all? Otherwise what would happen? – Quinty Jan 31 '15 at 21:06
  • Nothing would happen -- your code wouldn't compile. Again, please fix your posted code. – Hovercraft Full Of Eels Jan 31 '15 at 21:07
  • Oh, I think it makes more sense now. Thanks! I will fix it to be more specific – Quinty Jan 31 '15 at 21:07
  • Not to be more specific -- to make sense. – Hovercraft Full Of Eels Jan 31 '15 at 21:08
  • Your code at present says this `String a` [Ok, so variable `a` contains a String, great]. `a= new Card ()` [Ok, so we create a Card object, great, and we put a reference to it in variable `a`. Wait, didn't you just say `a` contained a String! This can't be right] – Richard Tingle Jan 31 '15 at 21:11
  • I never understood the concept of downvoting a question, answering in comments and asking the OP to answer his own question so that the downvote is removed. – gyc Jan 31 '15 at 21:11
  • Hopefully this makes more sense. Thanks everybody! – Quinty Jan 31 '15 at 21:11
  • @gyc: to post *real* code rather than sort-of kind-of code, yeah, it makes sense to me. And now his question is better. – Hovercraft Full Of Eels Jan 31 '15 at 21:13
  • put static keyword in function defination and you can call function by ClassName.functionNAme() – singhakash Jan 31 '15 at 21:15
  • @singhakash: he is asking **specifically** about instance methods, do let's not pollute the issue with statics. – Hovercraft Full Of Eels Jan 31 '15 at 21:15
  • @HovercraftFullOfEels: Interesting. Do you downvote every question that contains erroneous code? Don't you think it goes against the concept of a Q&A website? – gyc Jan 31 '15 at 21:21
  • @gyc: but seriously, the purpose of this voting is to motivate the questioner to ask better questions, and his code was critical to understanding just what his problem was, what his question was. Once he corrected his code by posting his real code, we all understood the question and many solutions were offered. To my mind, this is what voting on questions is all about -- down-voting questions that are unclear and up-voting questions that are well written and answerable. It's about posting code that clearly relates to the problem, and his first code bit didn't. – Hovercraft Full Of Eels Jan 31 '15 at 21:26

4 Answers4

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.

Luis Alves
  • 1,286
  • 12
  • 32
  • 1
    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. – Quinty Jan 31 '15 at 21:36
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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
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.

TedB
  • 346
  • 2
  • 5
  • 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. – Quinty Jan 31 '15 at 21:22
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);
barq
  • 3,681
  • 4
  • 26
  • 39
  • 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 Jan 31 '15 at 21:21
  • @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. – Basil Bourque Feb 01 '15 at 09:07