File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes instance methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "instance methods" Watch "instance methods" New topic
Author

instance methods

Ramm Kummar
Greenhorn

Joined: Feb 10, 2009
Posts: 26
How to call instance methods from the constructors as stated in this statement from the Kathy's book?

regarding constructor:::
You cannot make a call to an instance method, or access an instance variable,
until after the super constructor runs.

Instance variables can be used inside constructors but how to use methods?
Dejan Mratinkovic
Ranch Hand

Joined: Nov 20, 2008
Posts: 65
Can you please be precise what do you need?

You can use instance methods form constructor is if you would from any other method. Only, if you have call to constructor, it has to be first command in method.
Ramm Kummar
Greenhorn

Joined: Feb 10, 2009
Posts: 26
1 error) Why void type is not allowed here.
2 error) Why static method call should be first
3) Can we call static methods using object references?

init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\psbabu\SCJP6\build\classes
C:\Documents and Settings\psbabu\SCJP6\src\scjp6\Animal.java:11: 'void' type not allowed here
this(makeRandomName2());
C:\Documents and Settings\psbabu\SCJP6\src\scjp6\Animal.java:13: call to this must be first statement in constructor
this(makeRandomName());
2 errors
BUILD FAILED (total time: 0 seconds)


Dejan Mratinkovic
Ranch Hand

Joined: Nov 20, 2008
Posts: 65

C:\Documents and Settings\psbabu\SCJP6\src\scjp6\Animal.java:11: 'void' type not allowed here
this(makeRandomName2());

1 error) Why void type is not allowed here.


You have a method call (call to constructor of the Animal class) providing a response from void method as parameter.


2 error) Why static method call should be first

C:\Documents and Settings\psbabu\SCJP6\src\scjp6\Animal.java:13: call to this must be first statement in constructor
this(makeRandomName());


Static method call should not be first. About the error, check on wikipedia:

http://java.syntaxerrors.info/index.php?title=Constructor_call_must_be_the_first_statement_in_a_constructor

3) Can we call static methods using object references?


Yes, but not recommended.

Suggestion: Use IDE (I suggest Eclipse, but there are many good IDEs out there). Most of them are smart enough to solve all your problems (by suggesting fixes, etc.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance methods