Please explain constructor part. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain constructor part.

Here is the code I mange to complete the code but don't know how it is working import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); String movie = read.nextLine(); int row = read.nextInt(); int seat = read.nextInt(); Ticket ticket = new Ticket(movie, row, seat); System.out.println("Movie: " + ticket.getMovie()); System.out.println("Row: " + ticket.getRow()); System.out.println("Seat: " + ticket.getSeat()); } } class Ticket { private String movie; private int row; private int seat; //complete the constructor public Ticket(String a,int b,int c) { this.movie=a; this.row=b; this.seat=c; } public String getMovie() { return movie; } public int getRow() { return row; } public int getSeat() { return seat; } }

18th Jun 2022, 2:27 PM
Purushottam Dey
Purushottam Dey - avatar
1 Answer
+ 1
basically your constructor is the method that you call when you create an object. You use your constructor so you can initialize the value of your global variable which is movie,row and seat. For example, your parameter variable which is String a, its value would be assign to your global variable movie using this.movie = a
18th Jun 2022, 3:22 PM
Azalea