Quest static: Class can be used without instantiation?

Source: Internet
Author: User
Tags constructor static class

In general, when a class is needed, it must be instantiated before it can be invoked. In the process of programming, it is found that some classes can be used directly without instantiation, using its fields, methods, and so on.

At this time, depend on is static function. static English meaning "static, static", in OOP can be used as modifiers, classes, fields, properties, methods, and so on by static modification, static class, static field, static property, static method ...

The static-decorated class becomes a static class, which can contain only static members (fields, properties, methods) that are statically decorated, cannot be instantiated, cannot be inherited, and non-static can contain static members.

1, when called must first instantiate the situation:

The invoked member is a non-static member (at which point the class it belongs to must be a non-static class). As the following small example:

public class ClassA      //classa Class (Non-static Class)    
{public    
    ClassA () {}  //constructor public    
    void Fun () {}// The method in the ClassA class (Non-static method)    
} public    
        
class CLASSB     //needs to invoke the ClassB class    
{public    
    classb () {} of the method in the ClassA class {}  / /constructor public    
    void Fun ()      
    {    
        ClassA a = new ClassA ();//Calling Methods in the ClassA class need to instantiate    
        A.fun () first;    
    }    

Description: The ClassA class is a non-static class, where the method fun () is also a non-static method, so calling in CLASSB requires instantiating the ClassA class first.

2, the call does not need to instantiate the situation:

The called member is a static member (at which point it belongs to a static or nonstatic class). As the following small example:

(1) The called class is not a static class:

public class ClassA      //classa Class (Non-static Class)    
{public    
    ClassA () {}  //constructor public    
    static void Fun () {}// The method in the ClassA class (static method)    
} public    
        
class CLASSB     //needs to invoke the ClassB class {public    
    ClassB () {}/  /Of the method in the ClassA class. Constructor public    
    void Fun ()      
    {    
        classa.fun ();  Call the method in the ClassA class directly: Class name. Member    
    }    
}

Description: The ClassA class is a non-static class, but the method fun () is a non-static method, so when invoked in ClassB, the ClassA class (and cannot be instantiated) is called without instantiating it, calling its members directly, with the syntax "class name. Member".

(2) The called Class is a static class:

The public static class ClassA      //classa Class (static Class)    
{    
    //static class cannot have constructors public    
    static void Fun () {}// The method in the ClassA class (static method)    
} public    
        
class CLASSB     //needs to invoke the ClassB class {public    
    ClassB () {} of the method in the ClassA class  //constructor public    
    void Fun ()      
    {    
        classa.fun ();  Call the method in the ClassA class directly: Class name. Member    
    }    
}

Description: The ClassA class is a static class, where the method is also fun () as a non-static method, so when invoked in ClassB, the ClassA class (and cannot be instantiated) is called without instantiating the member directly, and the syntax is "class name. Member".

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.