Defined variables, constructors in the Java language

Source: Internet
Author: User
Tags define local

Day02 defining classes, variables, methods, constructors in the Java language


First, overview:

In the Java language, the definition and use of variables is very common and important, while the subsequent operating variables to lay the foundation, where the definition and use of variables will be used in our previous section mentioned in the data type, they are bound to use the two. Let's review the data types that we learned in the previous section.

After learning the variables, we should learn to use variables to define something, to construct some of the methods we need to meet the needs of learning. Thus the construction of this word is deduced. Construction is the meaning of creation, by creating something to satisfy. Here's a look at one by one.


Second, define "class":

The concept of "class" is derived from object-oriented, what is object-oriented? I'll just say, "All things are objects" and object oriented is to classify and program these things. Thus leads to the "class", is a kind of things defined in a class, convenient for the management and maintenance of programmers.

1, define the ordinary class:

public class student{} translation: Public: Is the access specifier, in subsequent chapters.            Class: Is the keyword that defines a class.            Student: This is the class name. {}: This is the subject, enclosed in curly braces

2. Package:

"Package" we can directly understand as a container, he is the class of the upper level of the directory, he is to be used to install classes, in the creation of the class will let us create the corresponding package. Note that the package name must be defined at the beginning of the creation class.

Package abc.com;         Package: Is the key word of the bag. ABC.com: is the specific registration and structure, refers to a class stored in the ABC folder under the COM folder.


Iii. defining "Variables "

"Variable" is the amount that can be changed, which we all know, you can define a variable you need to know what this variable is for, just to define? Or. So we have to know if it is necessary to define this variable. Define this variable and we need to know the type that defines it, is the integer type? is floating point type? Is it a string? Or what, that's all we have to think about.

For example:

int a=10; String b= "ABCDEFG"; float c=20f;doubl d=30d;

This is the definition of a variable. But variables in Java programming are divided into global variables and local variables, which is how they are standardized use? Here's a look at an example:

1. Define Global variables:

That is, variables defined within a class are global variables, and global variables can be used anywhere within a class.

public class One {//define global variable int a=10;    int b=20;  float c=30.5f; }

2. Define Local variables:

That is, a variable defined in a method is a local variable, and a local variable can be used within the method, a definition of a defined range.

public class One {//define a method public void student () {int a = 1;            int b = 2; }        }

3, the definition method:

A method is a vector that performs related operations by means of a method. The method is divided into a definition with parameters and no parameters.

Syntax for the definition:

The Access descriptor returns the type method name (parameter list) {};

3.1. Method definition without parameters:

public class One {//This is the class name public void Student1 () {System.out.println ("Student1 Hello!        ");//indicates output. }      }

3.2. Method definition with parameters:

public class One {//This is the class name public int student2 (int a, int b) {return a+b; }      }

Note--- return type:

When defining a method, be sure to note the return type, which is determined by the data type of the parameter, as shown in the following example:

public class One {//This is the class name public float student3 (int A, float b) {return a+b;      }}: Why is the return type a float data type?      Because you define a is an integer type, B is a floating point type, which is also marked with a decimal point. Does an integer plus a decimal number not be a decimal number? Therefore, the return type is floating-point.

4. Member variables:

For the concept of member variables, I will simply say, what is a member variable, that is, as long as some variables, methods, constructors defined in the class are the member variables of this class.


Example: simple to create a use

Creating the Student Class

Package www.com;    public class Student {//Students ' properties and types (define global variables) String number;    String username;  int age; Student Behavior Method 1 public void Take () {System.out.println ("the student has already started the Class!")      "); }//Student behavior Method 2 public void display () {System.out.println ("Study No.:" +number); System.out.println ("Name:" +username);      System.out.println ("Age:" +age); }        }

Create a Userstudent class to invoke the implementation output

        package www.com;         public class UserStudent {                     // Call the methods and variables defined by student.     public static void main (String[] args)  {     //main function, Main method. The entry for a program         //----------------------------access member variables------------------- First, create objects, Access class members     Student dzx;    dzx = new  Student ();//accessed by the object name. Property name   or   method name (). Also define assignment     dzx.number= "001";     dzx.username= "Dzx";    The  dzx.age=24;//assignment succeeds in calling his method to access the value of the output definition.     dzx.take ();     dzx.display ();     } } 

When you run the Userstudent class, you can view the defined data in Control Panel.


Four, the constructor function

"Constructor" It is also a core of learning Java, can solve a lot of problems, but also for our back system learning to do the relevant knowledge reserves.

1. How do I define a constructor function? Basic Syntax

Public Function name (argument list) {} Translate: public: access specifier, can omit not to write. Function Name: This name must be the same as the class name.            The difference is not a constructor function.            (): Parameter list, this is also can have no. {}: Function method body.

1.1. Constructors with no parameters

Package www.com;    public class Student {public Student () {number= "001"; username= "ZS"; age=24; } }

1.2. Constructors with parameters

Package www.com;  public class Student {public Student (String n,string name,int a) {number = N;username = Name;age =    A }     }


Example: Creating a Student class

Package www.com;    public class Student {//constructor 1, without parameters public Student () {number= "001";    Username= "ZS";        age=24;    }//constructor 2, with parameter public Student (String n,string name,int a) {number = n;    Username = name;        age = A; }        }

Creating the Userstudent Class

         package www.com;          public class UserStudent {                       public static  void main (String[] args)  {    //main function, Main method. An entry for a program                  / /second, create object access constructor 1    student stu = new student ();     Stu.display ();  //create an Object access constructor 2    student stu2 = new student ("002",   "WMZ",  22);     stu2.display ();              }                   }

When you run the Userstudent class, you can view the defined data in Control Panel.


V. Concluding remarks:

We're done here. Some definitions of classes, variables, methods, functions. The instance is also used. We can simply create a series of programs to run. Go find the examples on the books and try to do it well. It's very important.

This article from "Lonely One Night" blog, reproduced please contact the author!

Defined variables, constructors in the Java language

Related Article

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.