0

Actually I have two questions
1) Why we not create Object with simple mathod like [(class name) (object name;)] in java
2) Why we use Constructor to create object code below

class Run{}
 Run obj=new Run()

why not use this code
Run obj=new Run;
Please relese my confusion

Edited by moaz.amin.37: Wrong Code

3
Contributors
2
Replies
16
Views
3 Years
Discussion Span
Last Post by Slavi
0

because Java is a language with it's own syntax. the way you suggest wouldn't compile.
neither would the first, btw, you need to add a ';'
Run obj = new Run;
if this was valid, what if your class had two constructors? the jvm needs to know what it is you are trying to do, and for methods and constructors, it expects a list of parameters, which can go from 0 to ....
those, you put between the '()'

0

1) because java has its own syntax that has to be followed upon object creation
2) because you can initialize your class i nthe constructor so whenever you create an object of that class, it will already have assigned parameters and be ready to use(for example GUI's)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.