The parameterless parameter method of the class, the variable scope, the Javadoc document comment, the package

Source: Internet
Author: User
Tags function definition modifiers variable scope

How to define a class method

Public return value type method name () {

The body of the method

}

Returns: If there is a return value out of method, return result,

If a method has a return value, then calling this method will get the return value of the method.

Method names are usually used for hump nomenclature

Admin admin = new admin ();

Objects are created through a class, so the type of the object is Admin, which means the class is the type of the object

Admin. Method name (); Calling an argument-free construction method

Methods in the same class, call the method directly using a method name

Methods of different classes, first create the object, and then use the object name. Method Name () to invoke

member variables and local variables

Member variables:

1. Member variables are defined in the class and can be accessed throughout the class.

2. Member variables are created as the object is built and disappear as the object disappears, in the heap memory where the object resides.

3, the member variable has the default initialization value.

Local variables:

1, local variables are only defined in the local scope, such as: Inside the function, within the statement, etc., only in the area of the valid.

2, local variables exist in the stack memory, the end of the scope of action, variable space will be automatically released.

3. Local variables do not have default initialization values

The principles to be followed when using variables are: proximity principle

First in the local scope of the search, there is the use of, and then in the member position to find.

JavaDoc notes

Javadoc comments Start with/** */end

The development documentation of the program can be generated at the same time through Javadoc technology

JavaDoc's got a label.

To generate a Javadoc document:

Select the File --- export option, and the Export dialog box pops up to select JavaDoc in the Java menu. Option submission can

Defining the Parameter method

< access modifiers > return value type < method name > (< parameter list >) {

The body of the method

}

    The formal parameter appears in the function definition and can be used throughout the body of the function, leaving the function unused. The argument appears in the keynote function, and when it enters the function, the real parametric is not used. The function of formal parameters and arguments is for data transfer. When a function call occurs, the keynote function transmits the value of the argument to the parameter of the modulated function, thus realizing the transfer of the keynote function to the transferred function.    1. Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is valid only within the function. The parameter variable can no longer be used when the function call finishes returning the keynote function.     2. Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when making a function call, they must have a definite value in order to pass these values to the parameter. Therefore, an assignment, input, and so on should be used to obtain a definite value for the argument.     3. Arguments and formal parameters should be strictly consistent in order of quantity, type, or "Type mismatch" error will occur.     4. The data transfer that occurs in a function call is one-way. That is, the value of the argument can only be passed to the parameter, and the value of the parameter cannot be passed to the argument in reverse. Therefore, during a function call, the value of the formal parameter changes, and the value in the argument does not change.



Methods of arrays as parameters



The method that the object takes as a parameter
 public class Student {//Student class public int id;      public String name;      public int age;      public int score;      public void Showinfo () {System.out.println (id+ "\ t" +name+ "\ T" +age+ "\ T" +score);          } public class Studentsbiz {/** * Student Management class */student[] students = new STUDENT[30]; Add student public void Addstudent (Student stu) {for (int i = 0; i < students.length; i++) {if (Stu                 Dents[i]==null) {students[i]=stu;             Break         }}}//Show student information for this class public void showstudents () {System.out.println ("Student list of this class"); for (int i = 0; i < students.length; i++) {if (students[i]!=null) {students[i].showinfo ()             ;     }} System.out.println ();          } public static void Main (string[] args) {/** * object as a parameter of method * Example 5. * Instantiate student object and initialize */Student STUDENT1 = nEW Student ();           student1.id=10;           Student1.name= "Wang er";           student1.age=18;                      student1.score=99;           Student Student2 = new Student ();           student2.id=11;           Student2.name= "Zhang San";           student2.age=18;                      student2.score=100;           New Student Object Studentsbiz biz = new Studentsbiz ();           Biz.addstudent (STUDENT1);           Biz.addstudent (Student2);     Biz.showstudents (); }

Packages (Package)

  packages are used to classify classes that do different functions and place them under different directories (packages).

Name of the package

Naming rules for packages: reverse the company domain name as the package name.

  The package name is generally lowercase for each letter .

The class is defined under a package, usually at the beginning of the source file with the packages xxx.yyy;

The full name of the class is xxx.yyy. Class name

If you define a class without using the package name , Java considers the class to be in the default package .

Compile execution

If the package name is declared in the program, the first line is written in the packages com.xxx;

Then if you javac the source file name according to the general compilation method. java

The class file is generated in the current directory.

Then directly with: Java source file name execution, will be an error.

Appears Java.lang.NoClassDefFoundError

Cause of the problem

The meaning of a package is to produce a directory structure, so the class file must be in the appropriate directory hierarchy.

Take package com.xxx as an example:

One solution is to manually set up a COM folder, the inside of the XXX folder, the generated class file in the directory structure, and the implementation of the Java full class name (that is, with the package name) to be able to execute.

No need to manually create folders for resolution

Use the compile parameter-D workaround:

When compiling, you can use-D in the Javac command to specify the location where the generated class files are stored .

-D. Can be generated under the current directory.

Use the following command:

Javac–d. source file name. java

After compilation, in the current directory, the compiler generates the file directory hierarchy of the package and puts the class file in it.

If you do not use ".", you can also specify a directory.

When executing with Java commands, it is necessary to bring all the information of the package, which is executed in the form of a full add-on name.

Perform:

Java Full class name

Child Package

There are two package names, respectively, aa.bb.cc and AA.BB.CC.DD, so we call the latter the former child package .

Import

  Import, Import the various classes separated by the package, so that the compiler can find the required classes.

Classes that are under the same package do not need to be imported to be used directly, and classes under different packages need to be imported.

Use format:

Import AAA.BBB.CCC;

You can import all classes in a package by using the wildcard character "*" instead of the class name:

Import aaa.bbb.*;

Attention:

Import aaa.bbb.*, and the following class will not be imported into the AAA.BBB.CCC package.

Order problems

Questions about the order of the package, import, class:

1. The first need to define packages (package), optional;

2. Next use import, optional;

The definition of 3.class or interface.

Package Import and access rights

There are four types of access modifiers:

Public: All classes are allowed access.

Private: Allows access to this class.

Protected: Allows classes in this class, subclasses, and the same package to be accessed.

By default: The case of no access modifier allows the class and classes in the same package to be accessed.

After the corresponding package is import, it is only visible to the corresponding class, whether the members can be accessed or determined by the access modifier. Members that are decorated by default, even if the package of the corresponding class is imported, but cannot be accessed if the current class is not in the same package.

The parameterless parameter method of the class, the variable scope, the Javadoc document comment, the package

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.