cs61b class2--Intoduce to Java.Objects

Posted by phpeanuts on Tue, 02 Nov 2021 04:51:27 +0100

1. The method defined in class takes effect only when it is called in main(), e.g

public class Dog {
   public static void makeNoise(){

This is Dog.java, which has a method,makeNoise(), and then we can implement it in the DogLauncher.java file. The code is

public class DogLauncher{
    public static void main(String[] args){

It is worth noting that when using the command line, both Dog.java and DogLauncher.java need to be compiled with javac before they can run

This is the pit stepping point. At first, I only used javac DogLauncher.java, which will report an error

2. Instantiate class

Suppose we define a variable weight for Dog class to represent the quality of Dog,
public int weight
When calling in the DogLauncher class, we can use the keyword new, that is

Dog d;
d=new Dog();

In this way, instantiate a class, that is, d here

3. Constructor of class

Equivalent to c + +, you need a function with the same name as the class name as the constructor, so you can pass parameters to the constructor when initializing the class instance. The format is

public classname(type variable){


give an example

public class Dog {
 public int weight;
 public Dog(int w){

When we pass parameters in main(), we assign w to weight
So calling in main() is

Dog d;
d=new dog(20);

In this way, the weight is initialized to 20. When Dog() is instantiated as D, we call its makenoise () method to have the exact call. Use d.makeNoise() instead of the class name: Dog.makeNoise()

3.Array Instantiation, Arrays of Objects

Object array, similar to int[],String [], we can also declare Dog [], which means that it is an array of Dog class type

public class DogArrayDemo {
    public static void main(String[] args) {
        Dog[] dogs = new Dog[2];
        dogs[0] = new Dog(8);
        dogs[1] = new Dog(20);

We used the keyword new twice,
The first time is to open up space for the array, new Dog[2],
The second is to open up class space for array elements. dogs[0]=new Dog(8), which means instantiating the object array and passing parameters to weight=8

4. Comparison between static method and instance method (non static method)

Difference 1:
static method is not specific to a specific instance. Any instance calls it in the same structure, for example:

public class Dog {
    public int weightInPounds;
    public static String binomen = "Canis familiaris";

We define a scientific name for the Dog class, which is called Canis familiaris

Dog d1=new Dog();


Dog d2=new Dog();

The final result is that the dog's scientific name is Canis familiaris
In other words, the static method is not specific to a particular dog, but all dogs are the result
Difference 2:
The non static method is accessed by using the instance name,
The static method is accessed by using the class name,
In the above difference 1, it is actually wrong to use d1.binomen. Although it can be run, it is not allowed to use the instance name to call static methods.
Example call:
Static method: access with class name:

public static Dog maxDog(Dog d1, Dog d2) {
    if (d1.weight > d2.weight) {
        return d1;
    return d2;

This is the static method defined in the Dog class. We call it in main():

Dog d = new Dog(15);
Dog d2 = new Dog(100);
Dog.maxDog(d, d2);

Note that when you call maxDog, you use Dog.maxDog()
Access to non static methods:

public Dog maxDog(Dog d2) {
    if (this.weight > d2.weight) {
        return this;
    return d2;

There is no keyword static at this time. Above, we use the keyword this to refer to the current object. For example, you can call this method with the following command:

Dog d = new Dog(15);
Dog d2 = new Dog(100);

Note that maxDog() is called with the instance name d.maxDog()

Command line parameters

Think about it
public static void main(String[] agrs)
Meaning of each field
public: so far, almost all method s have added this prefix
Static: represents a static method, not for any specific instance
void: return value type, no value returned
main: function name
String [] args: array of type string, array name args

So if we print the first entry of the args array, what do we get?
The answer is that the error array is out of bounds because we have not passed any parameters. The correct way to use it is to pass parameters in the interpreter stage

javac hellow.java
java hellow 1 2 3

We passed the parameter "1 2 3" string, so args[0] is 1

Use library

Some Java libraries:

2.Princeton University:

Topics: Java