0

Sometimes we call className.methodName() without creating object for it, I mean without using syntax as className objectName = new constructor() and then call as object.methodName()

  1. When to use className.methodName()?
  2. When to call method using object as object.methodName()?

Explanation of above two cases with example will be appreciated.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • 2
    I assume you mean a `static` method. Consider having a look at [Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – MadProgrammer Aug 14 '15 at 04:52
  • Google "java static methods" – user253751 Aug 14 '15 at 04:55
  • Have you tried at least once to search on Google about your question? If you had tried it then you would have saved your time to type the question. – Naman Gala Aug 14 '15 at 05:05
  • possible duplicate of [How to call a method in java?](http://stackoverflow.com/questions/3713643/how-to-call-a-method-in-java) – Naman Gala Aug 14 '15 at 05:18
  • Google: [calling a method in java](https://www.google.com/search?q=calling+a+method+in+java&ie=utf-8&oe=utf-8) – Naman Gala Aug 14 '15 at 05:19
  • There are frameworks like Ofbiz that heavily use static method. Other like spring do not use them that much. Any answer given will be a mix of expertise and sheer opinion. – Hannes Aug 14 '15 at 06:14
  • 1
    The question is valuable for me. I am not a beginner of Java. But suddenly, I felt I am not clear the goodness of static creating instance method compared with constructors. Alexey Nikitenko 's answer help me to some degreee. haha. – Victor Choy Jan 09 '16 at 17:14

5 Answers5

2

What you're referring to is a static method.

Assume that I have this :

public class A {

    public static void foo(){
        System.out.println("Hooray! I work!");
    }

}

You can now do this anywhere else in any other class :

A.foo(); 

This is because the method is static, which means that it can be called on by the CLASS. This means that it doesn't require an instance of that class in order for the method to be called.

However, even though it isn't required, you can still do this :

A a = new A();
a.foo();

But since the method foo() is static, instantiating an object A is not required in order to run the foo() method.

Henry98
  • 571
  • 2
  • 12
2

First. When you're create at least one static method of a class, you can use this method without creating an instance of class. This is useful, for example, for the creation of methods with independent logic. For example:

 public class Checker {
        public static Boolean month(int value) {
            return (value >= 1 && value <= 12);
        }
    }

You need check correct value of month many times. But what to do each time to create the object. It is much effective to use a static method.

Second. When you create the object, the object is stored in the memory and you get a link to it. Then the object can be used for example to save at the list. Method at this object is specific. You can save class data and do specific operation with member of this class. For example:

List<Animals> animalsList = new ArrayList<>();
Animal animal = new Animal("dog");
int legs = animal.getCountLegs(); // specific function for object
animalList.add(animal); //save if you need
// use list of object
Alexey Nikitenko
  • 2,047
  • 2
  • 20
  • 30
0

For every class, we have a Object called as class object which is YourClass.class object. static methods are invoked based on meta-data on those objects. For instances of a class, methods are invoked on the actual instances. Both static and non-static methods are present on method area.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

There is no different between 1 and 2 point, because in during compilation compiler makes ClassName.staticMethod() instead of instance.staticMethod().

0

Static methods in java belong to the class (not an instance of it). They use no instance variables and will usually take input from the parameters, perform actions on it, then return some result. Instances methods are associated with objects and, as the name implies, can use instance variables.

Vinayak
  • 439
  • 4
  • 10