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

Recommended Answers

All 2 Replies

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 '()'

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.