Why do we have to say ArrayList twice?


#1

Why when making an object do we have to say the same thing twice? For example when making an array:

ArrayList<Integer> quizGrades = new ArrayList<Integer>();

Do the two ArrayLists mean different things? Is the first one the class and the second one a method? Or are they both just the class name?


#2

Thats simply the syntax for object declaration that was decided upon for use.
Think of it like this.
When you declare a variable
int something = 0;
That's a declaration for a simple variable.
Now extend that syntax to the object declaration.

LHS means
(Type of) objectname = new (you're making a new object from --->) this type of Class


#3

So will both sides always have the same thing?

Say if my code was:

public class Dog{
  public Bark(){
    System.out.println("Bark");
  }
}

would making an object from that be:

Dog newDog = new Dog();

what if I did:

Dog newDog = new Bark();

would that do anything or would it return an error?


#4

You're mixing up class declarations with methods. I can't remember that part of the lesson too well, but I'm certain that Dog was the only class that was used and Bark must have been a simple demo for a method (or function if you want to call it that)

You can only instantiate a class. That is, when you instantiate a class, you create the working object.
Classes are only templates.

Does that help ? Any more questions just ask. I'll be on line for another few hours anyway.


#5

I know that Bark is just a method I was just wondering cause in one part they had something like:

public class Car{
  public Car(){
    something here
  }
}

so when making it an object it was:

Car newCar = new Car();

which makes it a bit confusing using the same name for a method and class especially when beginners are trying to learn. Thanks for your help.


#6

Ok thats a different case again.
Now your talking about the constructor for the class.
The constructor has the same name as the class. It looks like a method because it is a method.

Sometimes you won't see it in a class code. This is because if the programmer does not explicitly define the code, the java interpreter will automatically create a default constructor.

You use a constructor to set up beginning (default) values in your class
If you dont assign values, the default values assigned by the interpreter will be null values.
Eg. int types are set to NULL, strings are set to NULL


#7

The constructor determines the type of the object that is created. Here, the ArrayList<Integer> on the left declares the type of the variable, quizGrades, and the new ArrayList<Integer>() on the right calls the constructor to create a new object of type ArrayList<Integer> that gets assigned to the variable.

ArrayList<Integer> quizGrades = new ArrayList<Integer>();

The two occurrences of ArrayList<Integer> in the same line of code are not redundant. In fact, when a constructor is called, and the new object is assigned to a variable, the type of the variable might not always be the same as the type of the object that is assigned to it. But, the type of the object assigned to the variable must either be the same as the type of the variable, or it must be a descendant of the type of the variable.

Following is an example. The file is named MainClass.java. Since a Dog is a type of Animal, based on the inheritance hierarchy, it is OK to assign a Dog object to an Animal variable.

class Animal {
    String name;
    public Animal(String name) {
        this.name = name;
    }
    public void eat() {
        System.out.println(this.name + " is eating animal food.");
    }

}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
    public void eat() {
        System.out.println(this.name + " is eating dog food.");
    }
    public void bark() {
        System.out.println(this.name + " goes ruff ruff!");
    }
}

public class MainClass {
    public static void main(String[] args) {
        Animal godzilla = new Animal("Godzilla");
        Dog rover = new Dog("Rover");
        Animal lassie = new Dog("Lassie");
        godzilla.eat();
        rover.eat();
        lassie.eat();
        rover.bark();
    }
}

Output:

Godzilla is eating animal food.
Rover is eating dog food.
Lassie is eating dog food.
Rover goes ruff ruff!

#8

Hi,

I try to finish this exercice but I don't understand this mistake on my code.
import java.util.ArrayList;

public class TemperaturesB {

public static void main(String[] args) {

	ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
	weeklyTemperatures.add(78);
weeklyTemperatures.add(111);
	weeklyTemperatures.add(67);
	weeklyTemperatures.add(89);
	weeklyTemperatures.add(94);

weeklyTemperatures.add(0,100);

System.out.println( weeklyTemperatures.get(2) );

}

}

I see Java says to me '111' but I have this message "Did you use the add method to add 111 at index 2?"


#9

Problem resolved :relaxed:


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.