-3

I am given a following partial Java code:

public static void main(String[] args) {
  var main = new Main();
  main.start();
}

I don't understand the initializing in Line 2 (Main()).

Also, what is the datatype of Main()? Suppose, I don't want to use 'var' keyword, then what should I use?

If there is any alternative code for this, please let me know.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ricky
  • 79
  • 1
  • 8
  • 2
    Is that all the code you have? – OneCricketeer Dec 01 '19 at 06:07
  • 1
    use Main instead of var (old type)? You could debug as well if you want to know the type, you could print the type as well. – SMA Dec 01 '19 at 06:07
  • Do take a look at https://stackoverflow.com/questions/44315657/what-is-the-exact-meaning-of-instantiate-in-java and https://stackoverflow.com/questions/12005584/initialize-object-directly-in-java – Naman Dec 01 '19 at 06:12
  • `new Main()` is creating an instance of `Main` and calling its constructor `Main()` with no arguments – user85421 Dec 01 '19 at 21:12

2 Answers2

5

I don't understand the initializing in Line 2 (Main())

You're initializing an object of type Main, in order to call the instance method start()

The alternative is to replace var with Main

Or simply new Main().start();

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

You can use something like this:

public static void main(String[] args) {
   Main main = new Main();
   main.start();
}
mohsenJsh
  • 2,048
  • 3
  • 25
  • 49