通过参考变量
通过方法
通过构造函数
class Student{
int id;
String name;
}
class TestStudent2{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Sonoo";
System.out.println(s1.id+" "+s1.name);
//printing members with a white space }
}
101 Sonoo
class Student{
int id;
String name;
}
class TestStudent3{
public static void main(String args[]){
//Creating objects Student s1=new Student();
Student s2=new Student();
//Initializing objects s1.id=101;
s1.name="Sonoo";
s2.id=102;
s2.name="Amit";
//Printing data System.out.println(s1.id+" "+s1.name);
System.out.println(s2.id+" "+s2.name);
}
}
101 Sonoo102 Amit
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){
System.out.println(rollno+" "+name);
}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
111 Karan222 Aryan
class Employee{
int id;
String name;
float salary;
void insert(int i, String n, float s) {
id=i;
name=n;
salary=s;
}
void display(){
System.out.println(id+" "+name+" "+salary);
}
}
public class TestEmployee {
public static void main(String[] args) {
Employee e1=new Employee();
Employee e2=new Employee();
Employee e3=new Employee();
e1.insert(101,"ajeet",45000);
e2.insert(102,"irfan",25000);
e3.insert(103,"nakul",55000);
e1.display();
e2.display();
e3.display();
}
}
101 ajeet 45000.0102 irfan 25000.0103 nakul 55000.0
class Rectangle{
int length;
int width;
void insert(int l, int w){
length=l;
width=w;
}
void calculateArea(){
System.out.println(length*width);
}
}
class TestRectangle1{
public static void main(String args[]){
Rectangle r1=new Rectangle();
Rectangle r2=new Rectangle();
r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
55 45
使用new关键字
通过newInstance()方法
通过clone()方法
通过反序列化
通过工厂方法等
new Calculation();
//anonymous object
Calculation c=new Calculation();
c.fact(5);
new Calculation().fact(5);
class Calculation{
void fact(int n){
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("factorial is "+fact);
}
public static void main(String args[]){
new Calculation().fact(5);
//calling method with anonymous object}
}
Factorial is 120
int a=10, b=20;
Rectangle r1=new Rectangle(), r2=new Rectangle();
//creating two objects
//Java Program to illustrate the use of Rectangle class which
//has length and width data membersclass Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void calculateArea(){
System.out.println(length*width);
}
}
class TestRectangle2{
public static void main(String args[]){
Rectangle r1=new Rectangle(),r2=new Rectangle();
//creating two objects r1.insert(11,5);
r2.insert(3,15);
r1.calculateArea();
r2.calculateArea();
}
}
55 45
//Java Program to demonstrate the working of a banking-system
//where we deposit and withdraw amount from our account.
//Creating an Account class which has deposit() and withdraw() methodsclass Account{
int acc_no;
String name;
float amount;
//Method to initialize objectvoid insert(int a,String n,float amt){
acc_no=a;
name=n;
amount=amt;
}
//deposit methodvoid deposit(float amt){
amount=amount+amt;
System.out.println(amt+" deposited");
}
//withdraw methodvoid withdraw(float amt){
if(amount<amt){
System.out.println("Insufficient Balance");
}
else{
amount=amount-amt;
System.out.println(amt+" withdrawn");
}
}
//method to check the balance of the accountvoid checkBalance(){
System.out.println("Balance is: "+amount);
}
//method to display the values of an objectvoid display(){
System.out.println(acc_no+" "+name+" "+amount);
}
}
//Creating a test class to deposit and withdraw amountclass TestAccount{
public static void main(String[] args){
Account a1=new Account();
a1.insert(832345,"Ankit",1000);
a1.display();
a1.checkBalance();
a1.deposit(40000);
a1.checkBalance();
a1.withdraw(15000);
a1.checkBalance();
}
}
832345 Ankit 1000.0Balance is: 1000.040000.0 depositedBalance is: 41000.015000.0 withdrawnBalance is: 26000.0