This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method question" Watch "method question" New topic

method question

Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
public class Foo
int counter = 10;
public void login()
int counter = 5;
System.out.println("The Local variable is : " + counter);

public void count()
System.out.println("The Instance variable is : " + counter);

public static void main(String[] args)
new Foo().login();
new Foo().count();

new Foo().login()
new Foo().count()
what is Foo()?
a method?...I don't understand the meaning of Foo().login()
thanks a lot.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Actually Foo() is a contructor of class Foo. Its a special type of method which has no return type and its name is same as class name. In java a constructor is required to instantiate any class. If i don't declare any constructor in the class then a default constructor is always there like in your case, a default constructor is there
Now if we declare any customized constructor which takes parameters or so then the default constructor vanishes we create the object using our own defined constructor.
Now to create the object of class Foo you will write the code
Foo any_obj_name = new Foo();
and to call any method of this object just use dot operator and method name.
In you example these 2 steps are performed in one. Made a new object without any name and then call method on that object.
new Foo().login();
I suggest you to read about constructors and other Object Oriented Concepts.
wood burning stoves
subject: method question
Similar Threads
compiles but doesn't work
how does this work?
synchronized thread???
The static synchronized methods of the same class always block each other as only one lock per class
creating object out of class of same name