Main method question

The question I have is about Java main methods in general. Up to now, my

public static void main(String[] args)

methods have only had to call methods that are also void, so there has not been a problem for me. Now I have to call a class constructor and I have no idea on how to call it from the public static void main(String[] args) method.

I have gone through my materials on hand, and I have looked it up on the internet. There are lots of references to using public static void main(String[] args) to call methods in a class, but I have found nothing on calling constructors specifically.

Thanks in advance for any tips on what I am missing. When it comes to learning Java, sometimes I feel I can't see the forest, just the trees. I keep getting this feeling that I am on the verge of the 'big' discovery on how Java works, but then I get bogged down in details.

[902 byte] By [DrDouga] at [2007-11-27 4:09:15]
# 1

new?

public class HelloWorld{

public static void main(String args[]){

HelloWorld greetings = new HelloWorld();

}

}

cotton.ma at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 2

You don't call constructors explicitly. You call new, and the constructor gets invoked implicitly.

public class Thing {

public static void main(String[] argv) {

Thing bob = new Thing();

}

public Bob() {

System.out.println("hello world");

}

}

When run, that should print "hello world" to the console.

paulcwa at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 3
**** you and your fast-typing fingers!
paulcwa at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 4
> **** you and your fast-typing fingers!Away for three months, come back and in four posts someone is yelling at me.Good times. Good times.
cotton.ma at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 5

That is what I thought too. Here is what I am running in to:

My class is overloaded with two constructors, one with default values: (Note: MyClass is the name for this constructor example only, I use the actual class name in my code!)

public MyClass()

{

this(Default_Depth, Default_Width);

}

And the 'main' constructor, which allows you to input your own depth and width variables:

public MyClass(int depth, int width)

{

//constructor body is here

}

When I set up my public static void main(String[] args) :

public static void main(String[] args)

{

MyClass newMyClass = new MyClass();

newMyClass.MyClass();

}

I get the error:

cannot find symbol - method Class();

I have a feeling the answer is staring me in the face, but I am missing it.

Thanks for the fast responses!!! :)

DrDouga at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 6

public static void main(String[] args)

{

MyClass newMyClass = new MyClass();

newMyClass.MyClass(); // what are you trying to do here?

}

See my comment above. I don't know what you are trying to do there. It's wrong whatever it is.

The quick answer would be to get rid of that line, but you might want to explain why you felt you needed it because then maybe we can steer you back on course.

cotton.ma at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 7
Oops, the line cannot find symbol - method Class();in my message above should read:cannot find symbol - method MyClass();Sorry, I am tying, not copy and pasting!
DrDouga at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 8
I want the public static void main(String[] args) to create an instance of this class/object to run from the command prompt.Thanks!
DrDouga at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 9
> I want the public static void main(String[] args) to> create an instance of this class/object to run from> the command prompt.> > Thanks!But the line before already does this...
cotton.ma at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 10

public static void main(String[] args)

{

MyClass newMyClass = new MyClass(); // this creates a new instance of MyClass

}

cotton.ma at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 11
And that is all I need then? Jeez am I dense...lolSo with the line:MyClass newMyClass = new MyClass();in the main, that is all? If that is the case then it is another example of me making things more complicated than they need to be.
DrDouga at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 12

I gave cotton_m four of the points, and paulcw got one. I am new to this points stuff (and the forum), so I hope this is ok with everyone. I appreciate the help in getting me to understand what I am trying to accomplish, and you guys turned on a lightbulb for me that I could not find the switch for.

Again, thanks!! :)

DrDouga at 2007-7-12 9:14:40 > top of Java-index,Java Essentials,New To Java...
# 13
> And that is all I need then? Well yes. You may want to call OTHER methods on the instance then, if you have any. I mean a program that just calls a constructor and then quits is not the most exciting thing in the world...But there isn't any more to creating the object.
cotton.ma at 2007-7-12 9:14:41 > top of Java-index,Java Essentials,New To Java...
# 14

> I gave cotton_m four of the points, and paulcw got

> one. I am new to this points stuff (and the forum),

> so I hope this is ok with everyone.

Thanks.

> I appreciate the

> help in getting me to understand what I am trying to

> accomplish, and you guys turned on a lightbulb for me

> that I could not find the switch for.

>

If you haven't seen this yet it's a good resource http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

cotton.ma at 2007-7-12 9:14:41 > top of Java-index,Java Essentials,New To Java...
# 15

That is what I figured out (with your help!). I added my method call and it runs from console perfectly! Now the main method makes complete sense to me:

public static void main(String[] args)

{

Create instance of object;

Call method(s) to run it;

}

I knew this, but the significance of the first line (create object) was what escaped me. I was treating the constructor as a method, and as I said, the answer was staring me in the face the whole time. With your help, I was able to understand my error, and for that I thank you! :)

One more hurdle behind me now! :)

DrDouga at 2007-7-21 21:02:48 > top of Java-index,Java Essentials,New To Java...