java / beginner
Snippet
Basic Class Constructors
A constructor is a special method that is called when an object is instantiated. It usually initializes the instance variables of the class.
snippet.java
1
2
3
4
5
6
class Player {String name;Player(String n) {name = n;}}
Breakdown
1
Player(String n) {
Declaration of the constructor with the same name as the class.
2
name = n;
Assigning the parameter value to the class variable.