cool hit counter Exploring static - classes work without instantiation?_Intefrankly

Exploring static - classes work without instantiation?


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/26224679

Normally, when a class is needed, it must be instantiated before a call can be made to it. In the course of programming, I found that some classes can be used directly without instantiation, using its fields, methods, etc.

at this time, It's all about...static role。static The English meaning is“ stationary, static”, (located) atOOP in may as a modifier, kind、 (numeric, data) field、 properties、 methods, etc. werestatic after modification, change into static class (as in electrostatic force)、 static field、 static property、 Static methods……

A class modified by static becomes a static class. static class (as in electrostatic force) in can only include Static members (fields, properties, methods modified by static), cannot be instantiated, cannot be inherited; in non-static may Contains static members.

1. The cases that must be instantiated first when invoked.

The called member is a non- Static members (At this point the class it belongs to must be non-static). The following small example.

     public class ClassA //ClassA class (non-static class)
    {
         public ClassA() { } // constructor
         public void Fun() { }//Methods in ClassA class (non-static methods)
    }

     public class ClassB // ClassB class that needs to call methods in ClassA class
    {
         public ClassB() { } // constructor
        public void Fun()  
        {
             ClassA a = new ClassA();// call the methods in ClassA class need to be instantiated first
            a.Fun();
        }
    }

Note: ClassA is a non-static class, and the method Fun() in it is also a non-static method, so you need to instantiate ClassA class first when calling it in ClassB.

2. Cases that do not require instantiation when invoked.

Called member is a static member (At this point the class it belongs to is a static or non-static class). The following small example.

(1) The called class is a non-static class.

     public class ClassA //ClassA class (non-static class)
    {
         public ClassA() { } // constructor
         public static void Fun() { }//Methods in ClassA class (static methods)
    }

     public class ClassB // ClassB class that needs to call methods in ClassA class
    {
         public ClassB() { } // constructor
        public void Fun()  
        {
             ClassA.Fun();   // Call the method in class ClassA directly: class name. members
        }
    }

Explanation: ClassA is a non-static class, but the method Fun() is a non-static method, so it is called in ClassB without instantiating ClassA (and cannot be instantiated), and its members are called directly, the syntax is "class name. members."

(2) The called class is a static class.

     public static class ClassA //ClassA class (static class)
    {
         // Of course there can't be a constructor in a static class
         public static void Fun() { }//Methods in ClassA class (static methods)
    }

     public class ClassB // ClassB class that needs to call methods in ClassA class
    {
         public ClassB() { } // constructor
        public void Fun()  
        {
             ClassA.Fun();   // Call the method in class ClassA directly: class name. members
        }
    }

Description: ClassA is a static class, and the method Fun() is a non-static method, so it is called in ClassB without instantiating ClassA (and cannot be instantiated), and its members are called directly, with the syntax "class name. members."

3. static modifier (expanded).

(1) Used to modify a class or a member of a class , which can be accessed without creating an instance (and cannot be instantiated), is automatically instantiated when called, and produces an instance in memory. When non-static classes containing static members instantiate objects, these objects share these static members and can access them either by class name or object name. I saw two interesting little examples online: Man is a non static class (as in electrostatic force)。 human brains , which is a property modified with static. This attribute is for all people and is a common characteristic of people. Not specific to a particular characteristic object (e.g. Zhang San has a brain, Li Si doesn't), because as long as a person is a person, he has a brain (although Carlos Rodriguez only has half a brain, and there are some people who are more brainy →_→, and that's considered having a brain), and since it's common to humans, he can't be instantiated.

Another example that can be instantiated is still the class human, the human height, Just a nonstatic particular property。 Because each person's height It's different.。 For example, I'm a big guy., reach up to1.55 meter (classifier), this one1.55 Rice is a description of my height, It's connected to me as a specific subject。 Yao Mingcai2.26 meter (classifier), Here's Yao's stats for this subject。 regardless of whether1.55 or2.26, It's all tied to a specific object, Rather than the traits common to humans。 therefore notstatic of may instantiated, but (not)static Cannot be instantiated。

(2) Modifying static variables inside methods: Our code is loaded from the hard drive into memory in It's the only way to run, in memory in There are three main areas, to store our code, Heaps, respectively.、 Stacks and static storage areas。 heap in The code is stored in of the reference type variable, If the instance of the class、 arrays etc.; wood or bamboo trestlework in The code is stored in The value type of the, as integer、 floating point、 Boolean etc.; Static storage area in Storing static and global variables、 ever bright。

The entire program runs with code that shares static variables in static storage, for example, defining the deposit balance as a static variable, the same bank card number, whether you are withdrawing money from the bank, withdrawing money from an ATM, or spending money online, using this Static balance.

So, the static variable inside the method, the value of the static variable does not disappear after execution, and when the method of this object is executed again, the value still exists, it is not allocated in the heap or stack, it is unassigned in the static area, this is the biggest difference with local variables. For memory allocation issues see The Difference Between Static Storage Areas, Heaps and Stacks


Recommended>>
1、Causes of JavaEE enterprise system performance problems and recommendations for solving them also applies to NET
2、Open source high impact resume example sentences see if youve been hit
3、Numbers Big Data Company Deqiang Farm
4、SoapUI testing WS interface handson
5、Small notes on file formats

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号