Compilation of the first Java program

Source: Internet
Author: User

Import java.io.*;

public class employeetest{

public static void Main (String args[]) {
/* Create two objects using a constructor */
Employee Empone = new Employee ("RUNOOB1");
Employee Emptwo = new Employee ("RUNOOB2");

Call the member methods of both objects
Empone.empage (26);
Empone.empdesignation ("Senior Programmer");
Empone.empsalary (1000);
Empone.printemployee ();

Emptwo.empage (21);
Emptwo.empdesignation ("rookie programmer");
Emptwo.empsalary (500);
Emptwo.printemployee ();
}
}
Class employee{
String name;
int age;
String designation;
Double salary;
Constructor for Employee class
Public Employee (String name) {
THIS.name = name;
}
Set the value of age
public void empage (int empage) {
age = Empage;
}
/* Set the value of designation */
public void Empdesignation (String empdesig) {
designation = Empdesig;
}
/* Set the value of salary */
public void Empsalary (double empsalary) {
salary = Empsalary;
}
/* Print information */
public void Printemployee () {
System.out.println ("Name:" + name);
System.out.println ("Age:" + ages);
System.out.println ("Position:" + designation);
System.out.println ("Salary:" + salary);
}
}

Results:

Name: RUNOOB1
Age: 26
Position: Senior Programmer
Salary: 1000.0
Name: RUNOOB2
Age: 21
Position: Rookie Programmer
Salary: 500.0

The main function is placed first, and a file contains at most one public, which is the class where the main function resides. This is a compilation method in a file, if the two classes are compiled separately to compile is the following look and method.

2.

A simple example

In this example, we create two classes:Employee and employeetest.

First, open the text editor and paste the following code in. Note Save the file as Employee.java.

The employee class has four member variables: name, age, designation, and salary. The class explicitly declares a constructor method that has only one parameter.

Employee.java File Code:Import Java.Io.*;Public Class Employee{ String Name;Int Age;String Designation;Double Salary;//Constructor for Employee class Public Employee(String Name){ This.Name=Name;} //Set the value of age Public void Empage(Int Empage){ Age=Empage;} /*Set the value of the designation*/ Public void Empdesignation(String Empdesig){ Designation=Empdesig;} /*Set the value of the salary*/ Public void Empsalary(Double Empsalary){ Salary=Empsalary;} /*Printing information*/ Public void Printemployee(){ System.Out.println("Name:"+Name );System.Out.println("Age:"+Age );Systemout. Println ( " jobs: " + designation ) system. Out. Println ( " Salary: " + salary) } } /span>

The program starts with the main method. In order to run this program, you must include the main method and create an instance object.

The following is a employeetest class that instantiates an instance of 2 Employee classes and invokes a method to set the value of a variable.

Save the following code in the Employeetest.java file.

Employeetest.java File Code:Import Java.Io.*;Public Class Employeetest{ Public Static void Main(String Args[]){ /*Create two objects using the constructor*/ Employee Empone=New Employee("RUNOOB1");Employee Emptwo=New Employee("RUNOOB2");//Call the member methods of both objects Empone.Empage(26);Empone.Empdesignation("Senior Programmer");Empone.Empsalary(1000);Empone.Printemployee();Emptwo.Empage(21) emptwo. Empdesignation ( " rookie programmer ") emptwo. Empsalary (500) emptwo. Printemployee} } /span>

Compiling these two files and running the Employeetest class, you can see the following results:

Employeetest.  employeetest  name:RUNOOB1 Age:  jobs: Senior programmer  Salary:1000.0  Name: RUNOOB2 Age:+  job: Rookie programmer  Salary:500.0


Compilation of the first Java program

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.