Class and Object in Java

byte       for whole numbers
short for whole numbers
int for whole numbers
long for whole numbers
float for floating numbers
double for floating numbers
char for single character(ACII table)
boolean logical - can be true or false

Class and Objects in Java

public class Student {
public String firstName;
public String lastName;
public int age;
}
public static void main(String[] args) {
Student student = new Student();
System.out.println(student); // Student@4617c264
}

Constructor in Java

public class Student {
public String firstName;
public String lastName;
public int age;
public Student(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
  • The constructor name is the same as the class name.
  • the constructor can have arguments similarly as methods do.
  • the class can have multiple constructors.
  • new Student("John", "Doe", 25); this part is using our constructor. It’s the same as with methods if arguments are matching constructor will get called.
  • The properties of our values are assigned by our constructor so we don’t need to assign one by one.
  • student uses constructor with three arguments.
  • student1 uses an empty constructor with no arguments.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store