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/