Some people think that the first Java program name is not a big problem, I don't think that is not a big problem, but when you write a lot of classes a day, you will find that the name is actually It's really important. If you have such a class, he needs to call several other classes. When these are called class names, the problem will appear, so how to distinguish them, and make their names not conflict? ?
After all, we can't remember the name of each statement, so that you can distinguish between other names next time. At this time, we need to use the package (package), a package is a namespace. The rules of the namespace are: reverse the Internet domain name, for example, my blog is blog.9cbs.com/manud You can use package manud.com.9cbs.blog in the beginning of the class, so it is better ~ Each package Is a unique namespace. Need to note: When using package to explain a package, the hierarchy of the package must be the same as the structure of the file directory! Like C: / MANUD / COM / 9CBS / Blog Using other component packages, how do you use it? In the beginning of the file, use the import keyword. If I want to import a Hello.Class component called I developed, I will write import manud.com.9cbs.blog.Hello; you can import the package I created, of course you also It can be lazy, use import manud.com.9cbs.blog. *; Import all the packages in this folder (maybe you have a package you can't use), but there is a drawback, it is compiled by directly specifying the package. The compilation speed is slow. Keyword Static (static) A class only When you use new to generate an object, his function can be called by the outside, but there are two mid situations that cannot be implemented with the above method: 1, no matter how much object or no When an object generates an object, some specific data storage only one; 2, some functions in the class do not want to bind together, that is, I don't want to generate objects, but I still want to use a function in class; in general If you want to use a non-static member, you must first generate an object. Use the object to call this data or function, so you have to know which object belongs to this function / data, because static is not generated Objects can be used, so the function or variable of Non-Static cannot be invoked directly in the Static function. Workaround 1: Place the Static keyword before the member variable or function definition, you can make them static.
Class Static I = 888; Public Static Void Main (STRING [] args) {staticDemo sd1 = new staticDemo (); staticDemo sd2 = new staticdemo (); system.out.println (sd1.i); // 888 System.out.println (SD2.I); // 888 staticDemo.i ; system.out.println (sd1.i); // 889 system.out.println (sd2.i); // 889}} Now even You produce n staticDemo objects, but i only the original copy, no matter if you are sd1.i or sd2.i, it is the same i, all references from this staticDemo.i, so as long as staticDemo.i stores Data, these SD1 and SD2 I will change because all of their handles points to STATICDEMO.I this memory space, which is equivalent to work in A, also works part-time in B, and he can't die If you fall, it will affect 2 companies, that is, this means, the same reason also applies to Static's function Solution 2: If you want to generate an object, you can use functions, so you need to add Static Class StaticDemo {Public Static Void show (string s) {system.out.println (s);} public static void main (String args []) {sho W ("one"); // call method 1 staticDemo.show ("two"); // Call method 2 new staticDemo (). Show ("three"); // Call method 3}} For call methods 1, In fact, he hides a keyword, which is this, his full form should be this.show ("one"), this THIS will be learned later. For calling method 2, use the form of classname.method (), this is the typical form of Static's function call, and the Non-Static function cannot be called using such a method! For calling methods 3, this method is the general way we call functions, first create objects, then call functions, this method is equally applicable to Static and Non-Static.
When a member variable is declared to static, its established approach has a large change, but the function of Static changes is not large, and the STATIC function is the point where the use of the STATIC function is that the function can call the function, just like We often see the main function. Your first Java program Java program defaults to import the package under java.lang into the file you created, we don't have to use Import to import. The name of a public class must be the same as the file name, and a Class is only allowed to have a public Class, otherwise the program is compiled. If you want to run this class, then there must be a main () function in this class, his fixed format is public static void main (String [] args), where PUBLIC means that this is a function that is open to the outside world. And is a Static's not allowed return value, and the parameters that are incoming Main () must be an array of String objects, even if you have never passed, this Args's String array can store the CMD parameters. Compiling and execute the Java file name .java to compile the java file, use java classname to perform classname this class, here you have to pay two points, the first is when you use the java command to execute a class, this class must have main Function; Second className does not necessarily equalize file names, if there is a public class in a java file, then this class name must be the same as the file name, and you can do your name as yourself. Notes and embedded documents // Note Units / *.....*/ Note Multi-line, multi-line comments cannot be nested multi-line comments.寓 文 档 档 注 注 注 j j 提 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件 文件Syntax, I have jumped over, interested friends, look at themselves, very simple coding style class name, should first write, if the name is composed of multiple words, put these words Write together and put their first letters, such as Public Class MyfirstClass. This chapter summarizes: this chapter is OK, huh, it is not difficult to speak this chapter. Everyone is to carefully read the book. I will experience the essence of OOP. This chapter is not difficult. Only one question is to use. After learning, we will put him as a problem. "Write a program so that it can accept 3 parameters incorporated by the command line. To do this, you need to index the string array representative of the command line (String args []) {System.out.println (args [0]); system.out.println (args [1]); system.out.println (args [2]);}} This program can accept 3 quotes, and take him Show out, some people will ask: How do we pass the quotes come in? Everyone is running, add a space behind Java Solve, plus three quotes, each with space separation, such as Java Solve AAA BBB CCC, this program will accept these three quotes, and display him.