Intended Learning Outcomes
- To understand relationship between objects and classes.
- To understand the use of constructors.
- To use private modifier to protect data fields
- To allow controlled access of private fields through getter or setter.
- To know the difference between instance and static variables and methods.
- To determine the scope of variables in the context of a class.
- To use the keyword this as the reference to the current running object
- To store and process objects in arrays.
Constructors
- Class is a construct that defines objects of the same type.
- A class provides a special type of methods, known as constructors, which are automatically invoked when an object is created.
- constructors mush have the same name as the class ifself
- constructors don’t have a return type
- Using new operator when an object is created
- Constructor play the role of initializing objects.You should place your code of initialization inside a constructor.
new Circle();//without args
new Circle(5.0);//with args
Default Constructor
- A class may be declared without constructors. So,a no-arg constructor with an empty body is implicitly declared in the class.
Automatically inserted by compiler
Declaring objects
similar to declaring a variable of primitive data types, you can declare a reference variable for an object,
Circle myCircle;
Circle myCircle=new Circle();//assign the object to a reference variable
Java assigns no default value to local variables inside method
在 Java 中,局部变量是定义在方法内部的变量,它们在方法执行时被创建,在方法执行结束后被销毁。对于局部变量,如果没有显式地初始化,Java 不会为其赋予默认值。
这意味着,在方法中定义的局部变量,如果没有赋初值,就不能直接使用,否则编译器会报错。这是因为在 Java 中,局部变量是不会自动赋初值的,必须在使用前进行初始化。
Different between Primitive Type and Object Type
- Primitive types include byte, short, int, long, float, double, char, and boolean.
They are pre-defined data types in the Java language used to store simple numerical or Boolean values.
Primitive types are characterized by their small memory footprint, fast access, inability to be null, and being passed by value. - Reference types include classes, interfaces, arrays, enumerations, etc. They are data types defined by programmers themselves and are used to store complex objects or data structures. Reference types are characterized by their large memory footprint, slow access, ability to be null, and being passed by reference.
- Storage: Primitive type values are stored directly on the stack, while reference type values are stored on the heap, with the stack storing the reference to the object.
- Default value: Primitive type variables are automatically initialized to their default values, such as 0 for int, while reference type variables default to null.
Data Encapsulation
Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a
restricted set of operations on the hidden (private) members of the class by executing special
(public) methods. As a result:
- Data fields are being protected from illegal access
- Maintenance of a class becomes easier in the long run
Immutable
-
在 Java 中,像 String、Integer、Boolean 等基本数据类型的包装类都是 immutable 的。一旦创建了这些对象,就不能再改变它们的值。
-
A class with all private data fields and without mutators(setter method) is not necessarily immutable.
-
For a class to be immutable, it must:
- make all data fields private;
- provide no setter(mutator)methods;
- provide no getter(accessor)methods that would return a reference to a mutable data field object.
- [may provide getter (accessor) methods to allow read-only access to its data fields, but these methods should not return a reference to a mutable data field object. Instead, they should return a copy of the data field or an immutable object that represents the data.]
-
在 Java 中,可以通过以下方式创建一个 immutable 对象:
将变量声明为 final,确保其值不会被修改。
将变量声明为 private,通过提供 getter 方法来访问其值。
使用不可变的集合类,例如:
Collections.unmodifiableList()、Collections.unmodifiableSet()、Collections.unmodifiableMap() 等。