Java class constructor list example
Java class constructor list example
I'm happy to help! Here's a detailed explanation of constructors in Java with examples:
What is a Constructor?
A constructor is a special method that gets called when an object is created from its class. It has the same name as the class and no return value (not even void
). A constructor is used to initialize objects, set their properties, and perform other necessary setup.
Constructor Syntax
The syntax for a constructor in Java is as follows:
public Classname(parameter1, parameter2, ...) {
// Constructor body
}
Where Classname
is the name of the class, and parameter1
, parameter2
, etc. are the parameters passed to the constructor.
Example 1: Simple Constructor
Let's create a simple example with a Person
class that has two properties: name
and age
.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
To use this constructor, we would create a new Person
object like this:
Person person = new Person("John", 30);
The constructor would then set the name
and age
properties of the person
object.
Example 2: Constructor with Multiple Parameters
Let's add another property, address
, to our Person
class. We'll also create a constructor that takes three parameters: name
, age
, and address
.
public class Person {
private String name;
private int age;
private String address;
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
}
To use this constructor, we would create a new Person
object like this:
Person person = new Person("John", 30, "123 Main St");
The constructor would then set the name
, age
, and address
properties of the person
object.
Example 3: Constructor with No Parameters
Sometimes we might want to create a default constructor that takes no parameters. We can do this by creating a constructor with an empty parameter list (()
).
public class Person {
private String name;
private int age;
public Person() {
this.name = "Unknown";
this.age = 0;
}
}
To use this constructor, we would create a new Person
object like this:
Person person = new Person();
The constructor would then set the default values for the name
and age
properties.
Example 4: Constructor with Default Values
We can also create constructors that take parameters with default values. For example, let's say we want to create a Car
class with three properties: make
, model
, and color
. We'll create a constructor that takes two parameters (make
and model
) with default values for the color
property.
public class Car {
private String make;
private String model;
private String color;
public Car(String make, String model) {
this.make = make;
this.model = model;
this.color = "White"; // default value
}
}
To use this constructor, we would create a new Car
object like this:
Car car = new Car("Toyota", "Corolla");
The constructor would then set the make
, model
, and color
properties of the car
object.
These are just a few examples of how constructors can be used in Java. Constructors are an important part of object-oriented programming, as they allow us to create objects with specific initial values.
Java List constructor with elements
The List
class in Java provides several constructors to create a list with initial elements. Here are the commonly used constructors:
1. ArrayList(E... elements)
: This is the simplest way to create an ArrayList
with initial elements.
import java.util.Arrays;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
2. LinkedList(E... elements)
: This is similar to the first constructor, but it creates a LinkedList
instead of an ArrayList
.
import java.util.Arrays;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
3. LinkedList(Collection<? extends E> c)
: This constructor creates a LinkedList
from a given collection.
import java.util.Arrays;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
4. Vector(E... elements)
: This is similar to the first constructor, but it creates a Vector
instead of an ArrayList
. However, please note that Vector
is a legacy class and not recommended for use in new code.
import java.util.Arrays;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
Vector list = new Vector<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
5. CopyOnWriteArrayList(E... elements)
: This constructor creates a CopyOnWriteArrayList
with initial elements.
import java.util.Arrays;
import java.util.CopyOnWriteArrayList;
public class Main {
public static void main(String[] args) {
CopyOnWriteArrayList list = new CopyOnWriteArrayList<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
6. AbstractSequentialList(E... elements)
: This is a abstract constructor that can be used as a base class for your custom list implementation.
import java.util.Arrays;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
MyList list = new MyList<>(Arrays.asList("Apple", "Banana", "Cherry"));
System.out.println(list);
}
}
class MyList extends AbstractSequentialList {
// Your implementation goes here
}
Note that the Collection
class itself does not have a constructor for creating a list with initial elements. However, you can use other collection classes like ArrayList
, LinkedList
, etc., as shown above.
In summary, Java provides several constructors to create lists with initial elements. The choice of constructor depends on the specific requirements and constraints of your code.