title: Creating-Java-Classes
date: 2023-11-05	
author:
  - AllenYGY
status: DONE
tags:
  - NOTE
  - Java
  - Lec3
  - Program
created: 2023-11-05T00:59
updated: 2024-05-31T01:01
publish: TrueCreating-Java-Classes
A class is a programmer-defined type.
A value of a class type is called an object or an instance of the class: an object exhibits the properties and behaviors defined by the object’s class.
Objects have data and behavior.

In java we use constructors to construct new instances.
public ClassName(Parameters){ code }
The new operator is used in combination with constructor to create an object from a particular class.
> ClassName classVar= new ClassName();
In Java, the value of any object variable is a reference [2] to an object that is stored elsewhere.
Person person1 = new Person();
Person person2 = person1;
Person person3 = null;       //refers to no object

person1.name // "." is the period operator
person2.age
- instance variables store the data of the object.
- Each object has its own copy of the variables.
- Every object has a state that is determined by the values stored in the instance variables of the object.

A variable declared within a method definition is called a local variable. 定义在方法里的变量
It(this) works as a reference to the current Object, whose method or constructor is being invoked.
public class Person{
    private intage;
    private String name;
    public Person(){
        this("Bob",0);
        System.out.println("Inside Constructor without parameter");
    }
    public Person(String name, intage){
        this.name = name;
        this.age= age;
        System.out.println("Inside Constructor with two parameters");
    }
}
public class Person {
    private intage;
    private String name;
    public Person(String name, intage) 
    {
        this.name = name;
        this.age= age;
    }
    public void method1(Person pp) 
    {
        System.out.println("Inside Method1");
        System.out.println("Age:"+pp.age);
    }
    public void method2() 
    {
        this.method1(this);
        System.out.println("Inside Method2");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person p = new Person("Bob",23);
        p.method2();
    }
}

