Constructor v Method

I am new newbee and can some one tell me what the difference between a constructor() and a method () is ?Thanks
[125 byte] By [hank__a] at [2007-10-2 14:02:07]
# 1
A constructor is run once upon object creation.
hank__a at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 2
Break it down once more Chief :P
hank__a at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 3

Hi Hank...

"Constructor provides information about, and access to, a single constructor for a class." Java Definition...

When you have a class say MyClass.java

And you want to create an instance of it..

MyClass thisClass = new MyClass();

What that does now is that it calls the constructor of that class... Automatically...

For instance here is MyClass class....

class MyClass {

// Make a private String for this class only

private String theStr = "";

// First constructor only takes no arguments

public MyClass() {

this.theStr = "No Arguments";

}

// Lets make another constructor that takes a String as a argument

public MyClass(String str) {

this.theStr = str;

}

// Now the method....

public void myMethod() {

// Which would print the classes String...

System.out.println(this.theStr);

}

}

And to call that we just do this.. from any other class or from main

// Create the object of MyClass *NOTE no parameter given....

MyClass onlyClass = new MyClass();

onlyClass.myMethod();

MyClass nextClass = new MyClass("OMG THIS IS SO COOL");

nextClass.myMethod();

So what will this print? So upon instantiating a class, the constructor of that class is called automatically....

the result of the above would be...

No Arguments

OMG THIS IS SO COOL

I hope that helped..

A Method is methods you call after creation of a class was set..

MohamedMansoura at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 4

> Break it down once more Chief :P

Read a frickin' tutorial.

[url=http://java.sun.com/docs/books/tutorial/]Sun's basic Java tutorial[/url]

[url=http://java.sun.com/learning/new2java/index.html]Sun's New To Java Center[/url]. Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.

[url=http://javaalmanac.com]http://javaalmanac.com[/url]. A couple dozen code examples that supplement [url=http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]The Java Developers Almanac[/url].

[url=http://www.jguru.com]jGuru[/url]. A general Java resource site. Includes FAQs, forums, courses, more.

[url=http://www.javaranch.com]JavaRanch[/url]. To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.

Bruce Eckel's [url=http://mindview.net/Books/DownloadSites]Thinking in Java[/url] (Available online.)

Joshua Bloch's [url=http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]Effective Java[/url]

Bert Bates and Kathy Sierra's [url=http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance]Head First Java[/url].

James Gosling's [url=http://www.bookpool.com/sm/0321349806]The Java Programming Language[/url]. Gosling is

the creator of Java. It doesn't get much more authoratative than this.

jverda at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 5
> Break it down once more Chief :PSure,1. A constructor is not a method.2. A constructor is run once when the object is created then nevermore. It cannot be "called" like a method. It has no return type.
jverda at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 6
Thank you very much! I now understand, thanks to everyone apart from Mr. jverd
hank__a at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 7
You're welcome.
jverda at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...
# 8

> Thank you very much! I now understand, thanks to

> everyone apart from Mr. jverd

jverd gave you valuable resources to review. Your only beef could be the tone in which it was given, but you very much warrant such a tone, being the research-challenged spoon-feed-me nincompoop that you appear to be.

warnerjaa at 2007-7-13 12:09:36 > top of Java-index,Java Essentials,New To Java...