A strange problem: declare an object of this class in the class

Source: Internet
Author: User

Things originated from three Java programs (please allow me to make no difference between Java and j2se): When we compile a small Java console program, we can use the following three styles: first, directly use the public class to directly write the required methods and member variables in the public class. Because the main method is static, our methods and member variables are all written as static. Import Java. io. *; public class test {static int mynum = 20; public static int myfunction () {return mynum + 1;} public static void main (string [] ARGs) {system. out. println (mynum); system. out. println (myfunction () ;}second: directly create a public class object in the public class, so that all methods and member variables do not have to be static, this method is too strange. Import Java. io. *; public class test {int mynum = 20; Public int myfunction () {return mynum + 1;} public static void main (string [] ARGs) {test myclass = new test (); system. out. println (myclass. mynum); system. out. println (myclass. myfunction () ;}}third: it is also the most normal one. Create a class and create a new class in the public class. Import Java. io. *; Class mytest {int mynum = 20; Public int myfunction () {return mynum + 1 ;}} public class test {public static void main (string [] ARGs) {mytest F = new mytest (); system. out. println (F. mynum); system. out. println (F. myfunction () ;}} the second method is "New Class Object in the class". See the following program: Import Java. io. *; Class mytest {int mynum = 20; Public int myfunction () {return mynum + 1;} mytest T = new mytest (); // new class object in the class} Public class test {public static void main (string [] ARGs) {mytest F = new mytest (); system. out. println (F. mynum); system. out. println (F. myfunction () ;}} is compiled, but cannot run normally. Why should I allow new class objects in the class? The following program is correct: Import Java. io. *; Class mytest {int mynum = 20; Public int myfunction () {mytest T = new mytest (); Return T. mynum ;}} public class test {public static void main (string [] ARGs) {mytest F = new mytest (); system. out. println (F. mynum); system. out. println (F. myfunction () ;}} a Java program is composed of classes. In a Java program that can be run independently, a public class is required, the public class must contain a static main method as the entry point of the program. Because the main method is static, it belongs to a class rather than an instance. In other words, we can place the main method in any class (Public of course) at will ). Since Java is object-oriented, why is it so weird? Can't the main method be extracted as independent? No matter what the object orientation is, we always need an unknown instance to come out first. Otherwise, it becomes a problem of having a chicken or an egg. (Although I know little about object-oriented, I still think that static itself is not in line with the idea of object-oriented, because the instance does not exist, and static already exists-this is an external question. ^_^) back to our issue, it seems that the logic of a new class instance (or an object) in the class itself cannot be understood? Let's look at the following program: Import Java. io. *; Class mytest {int mynum = 20; mytest () {mynum ++; mytest T = new mytest ();} public int myfunction () {mytest T = new mytest (); Return T. mynum ;}} public class test {public static void main (string [] ARGs) {mytest F = new mytest (); system. out. println (F. mynum); system. out. println (F. myfunction () ;}}let's take a look at how C ++ works. The following program cannot be compiled (gcc2.95.2): # include <iostream> class test {public: int num; Test temp; temp. num = 20 ;}; int main (INT argc, char * argv []) {test mytest; cout <mytest. num <Endl; return 0;} is normal in the following program: # include <iostream> class test {public: int num; int func () {test temp; temp. num = 20; return temp. num ;}; int main (INT argc, char * argv []) {test mytest; cout <mytest. func () <Endl; return 0;} Forget it. Do not write the code properly.

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.