Can I use methods of a class without instantiating this class?

前端 未结 11 2084
执念已碎
执念已碎 2020-12-15 04:10

I have a class with several methods and there is no constructor among these methods.

So, I am wondering if it is possible to call a method of a class without a creat

相关标签:
11条回答
  • 2020-12-15 04:45

    I have a class with several methods and there is no constructor among these methods.

    If you don't explicitly define a constructor then you get a default constructor provided by the compiler. So if those methods aren't static, try this:

    NameOfClass x = new NameOfClass();
    x.doMethod(x1,x2,...,xn);
    
    0 讨论(0)
  • 2020-12-15 04:46

    That would be static methods.

    0 讨论(0)
  • 2020-12-15 04:49

    In proper encapsulation, you should not "see" what is happening upon instanciation. To rely on a class's lack of a constructor is breaking this form. The designed of the class my have in mind to add formal state initialization in the constructor at a later date. Your "contract" with the class is only that you can use the methods as they are currently designed.

    If you desire to use the functionality of that method without the class overhead, maybe it best for you to include that method in your existing "client" class (of course this is just "copy and paste" coding and is considered an anti-pattern of of software design.

    0 讨论(0)
  • 2020-12-15 04:52

    1) YES, you can use the methods of a class without creating an instance or object of that class through the use of the Keyword "Static".

    2) If you declare the method as "Static" then you can call this method by :

                    *ClassName.MethodName()* 
    

    3) E.g.

    class Hello {
    
       public static void print()
       {
    
         System.out.println("HelloStatic");
       }  
    
    }
    
    
    class MainMethod {
        public static void main(String args[])
        {
           // calling static method
           Hello.print();
        } 
    }  
    

    4) The output of the above program would be : HelloStatic

    0 讨论(0)
  • 2020-12-15 04:58

    A method on a class operates in the context an instance; it has access to the instance's member variables. You understand this, because you ask about what happens if the method accesses one of those variables.

    You can understand why it doesn't work by asking yourself the question: "Where is the data?" If you don't have an instance, where is the instance variable? Where is the data? And the answer is that it doesn't have a place and therefore doesn't work.

    The difference with a static function and static member variables is that you can answer the question about the location of the data. The static variables available regardless of whether there is a specific instance or not. The instance specific vs. class specific decision is one that you must make considering what you actually want to do.

    0 讨论(0)
  • 2020-12-15 05:00

    As many have pointed out: This is only possible if the method is static. Maybe some OOP background is in order: A method should always belong to a class. So what is the use of calling a method without an instance of a class? In a perfect OO world there shouldn't be any reason to do that. A lot of use cases that have to do with static methods talk about assigning some kind of identity to your class. While this is perfectly reasonable in a programming world it isn't very convincing when it comes to object oriented design.

    As we program in an imperfect world there is often a use case for a "free function" (The way Java or C++ implement sort() for example). As Java has no direct support for free functions classes with only static "methods" are used to express those semantics with the added benefit of the class wrapper providing a "namespace". What you think of this workaround and if you see it as a flaw in language design is IMO a matter of opinion.

    0 讨论(0)
提交回复
热议问题