0

Whenever I have to call a method from another class, I first create an object and then call it through the object. But while I was writing some code, I mistakenly wrote classname.methodname(); and it worked.

I would usually write,

classname obj = new classname();
obj.methodname();

Here is the actual code:

Class 1

public class Dataset {

    public static List<ECCardData> getDataset() {
         //Code
}

in Class 2

List<ECCardData> dataset = Dataset.getDataset();

I noticed that the methodname() was static. Was that the reason?

Vedant
  • 422
  • 1
  • 3
  • 21
  • 3
    I would assume because `methodname` was declared static. It's hard to guess why that is the case without any knowledge of the class structure. – Ben May 30 '18 at 07:24
  • I assumed the same, Let me update the question with the code I have at hand @Ben – Vedant May 30 '18 at 07:25
  • Also I misread your question, my bad. Yes, that is because it is static. Refer [here](https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method) for more information. – Ben May 30 '18 at 07:26
  • 1
    You may want to go through this: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Stultuske May 30 '18 at 07:26
  • You should call static methods with the class name. Calling them via an instance e.g. `instance.staticMethod()` is considered bad form because it makes it look like you're calling an instance method. – David Conrad May 30 '18 at 07:36
  • 1
    Or [What does the 'static' keyword do in a class?](https://stackoverflow.com/q/413898/4391450) – AxelH May 30 '18 at 07:51

2 Answers2

0

Yes, for static methods (with suitable access modifier) you can call directly with your class by

YourClass.yourMethod();

and this way, too

YourClass anObject = new YourClass();
anObject.yourMethod();

Happy coding.

Johnny Ngo
  • 104
  • 4
0

I hate answering my question, but I found the correct answer.

When a method is declare static, only one instance of that method will exist. When you create an object a new instance of the method is created, which is not possible for a static method. Therefore you use the class name.

classname.methodname(); //only one instance 

classname obj;
obj.methodname(); //instance with obj as Object(IDE gives warning, should not be allowed, ideally)

The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.

However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.

You can do this to execute a static method:

classname.staticMethod();//Simply refers to the class's static code But
> to execute a non-static method, you must do this:
> 
> classname obj = new classname();//Create an instance
> obj.nonstaticMethod();//Refer to the instance's class's code
Vedant
  • 422
  • 1
  • 3
  • 21
  • There's nothing wrong with answering your own question on Stack Overflow. But be aware that the second form is considered a bad practice if `methodname()` is a static method, because it makes it look as if it was an instance method. – David Conrad May 30 '18 at 07:37
  • Yes, It is not possible to have an instanced static method, let me update the answer – Vedant May 30 '18 at 07:38
  • no instance of method is created, – Hemant Patel May 30 '18 at 07:41
  • no instance of method is created, – Hemant Patel May 30 '18 at 07:41
  • A method has no instance. – Khan Saab May 30 '18 at 07:41
  • @KhanSaab a static method has one instance, a non-static method can have multiple instances – Vedant May 30 '18 at 07:43
  • @Vedant A method has no instance, An instance can be created with `new` key word in java. Static method is a property of a class not of an object(instance) of that class. – Khan Saab May 30 '18 at 07:46
  • You do not correctly use the terms _instance_ and _method_. There simply are no instances of methods. You can have instances of classes, though. Technically, a method's definition existst only once in memory, even if it is an instance method. And speaking about methods, we distinguish between instance methods and static methods (or class methods). – Seelenvirtuose May 30 '18 at 07:46