wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes getting output without creating object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting output without creating object ?" Watch "getting output without creating object ?" New topic
Author

getting output without creating object ?

Nipun Devlekar
Greenhorn

Joined: Dec 07, 2005
Posts: 9
I tried to execute the following code from SCJP book

In the above code the author did not create an object for eg;Good a = new Good() and has directly called the constructor followed by the method.The above does not seem to be the normal convention to write a program.
Can anyone explain to me how the above code gets executed without creating an object reference?
Can one write a method inside a constructor and execute it as we do it for object?I wrote the following code which reported the following errors

errors were
devtest$ javac Good.java
Good.java:5: '}' expected.
System.out.println("Constructor");
^
Good.java:7: Statement expected.
void Bad() {
^
Good.java:14: Identifier expected.
public static void main(String[] args) {
^
Good.java:14: 'class' or 'interface' keyword expected.
public static void main(String[] args) {
^
4 errors
Can anyone solve my doubt
Thanks in advance
NIPUN.
[ December 19, 2005: Message edited by: Nipun Devlekar ]
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Hi Nipun,

In the above code the author did not create an object for eg;Good a = new Good() and has directly called the constructor followed by the method.The above does not seem to be the normal convention to write a program. Can anyone explain to me how the above code gets executed without creating an object reference?

Object of class Good is created. There is nothing wrong in this code. Even from constructor it's legal to call any method, however it's not a good practice.

Can one write a method inside a constructor and execute it as we do it for object?I wrote the following code which reported the following errors
You can not declare or define any method inside a constructor.

hth,
Harshil
Nipun Devlekar
Greenhorn

Joined: Dec 07, 2005
Posts: 9
looking at the code i posted earlier above; i tried to incorporate the coding technique in the following codes.Theere are 3 codes one is super class,one is subclass and one is mainline.I found that i am not able to execute a method of subclass eg new PrintableCharStack(10).input() but am able to do so for super class eg; new PrintableCharStack(10).getInstanceCount().following are the codes





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting output without creating object ?