*
The moose likes Beginning Java and the fly likes Example for Certification book doesnt work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Example for Certification book doesnt work" Watch "Example for Certification book doesnt work" New topic
Author

Example for Certification book doesnt work

Vaibhav B Sharma
Greenhorn

Joined: Nov 28, 2011
Posts: 7

Hi,

I am newbie to java, started learning after completing 7 years in Cobol development. I am preparing for Certification exam, and need your help in following.

I copied the sample code from SCJP 6 guide to play around with it, and it doesn't run in my machine. Can someone assist. The code is taken from Chapter 2, question 8 from section Q&A:Self Test.

Error that I get in eclipse is

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Dogshow cannot be resolved to a type
at TestQ.DogShow.main(DogShow.java:5)

Thanks !

Code -


Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Hi Vaibhav,
Welcome to JavaRanch
You have a class declared as DogShow but in the main method you are referring to it as Dogshow. So the compiler is not able to find class of Dogshow type.


Mohamed Sanaulla | My Blog
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

And apart from that, the way the objects are created is- you specify the new operator followed by a call to the constructor which initializes the object. By default if you dont specify any constructor Java provides a default no-arg constructor like:


the above is equivalent to:

because the compiler adds the no-arg (no argument) constructor to the class.

Now you create instance of your class by:


in case of methods which are static, you would not need an instance to invoke them and can use:


otherwise you would require an instance to invoke non-static methods, something like


You would have to edit your code at 2 places where you are instantiating a class.
Vaibhav B Sharma
Greenhorn

Joined: Nov 28, 2011
Posts: 7

Hi,

Thanks for quick response, and sorry, that was my typo. But it still doesn't work for anything that I declare as an object without assigning to reference type. There are two such errors that I am getting -

C:\JAVA\Packone\Source>javac DogShow.java
DogShow.java:5: error: cannot find symbol
new DogShow.go();
^
symbol: class go
location: class DogShow
DogShow.java:8: error: cannot find symbol
new Hound.bark();
^
symbol: class bark
location: class Hound
2 errors

Code as per below -

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Vaibhav B Sharma wrote:Hi,

Thanks for quick response, and sorry, that was my typo. But it still doesn't work for anything that I declare as an object without assigning to reference type. There are two such errors that I am getting -

I think I pointed out this issue in my second post. As I overlooked the way you were creating the objects in my first post.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

So the way you are creating the instance would be:
Vaibhav B Sharma
Greenhorn

Joined: Nov 28, 2011
Posts: 7

Sorry Mohamed, didn't see your complete reply before posting my query. I still have a few questions -

1. Since I need an instance to invoke non-static method, I am creating one already -

new Hound.bark();

It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();

but why doesn't it work if I just create and provide an instance of Hound on which I want to run bark() on ? I don't want to use reference variable, since I don't care if object is lost after method is complete.


2. This coding technique is used in many examples in SCJP 6 certification guide. So am I missing something here ?

Thanks again for your prompt response.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Vaibhav B Sharma wrote:
1. Since I need an instance to invoke non-static method, I am creating one already -

new Hound.bark();

It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();


You are missing the (). new Hound().bark(); is the correct syntax for creating the object.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Vaibhav B Sharma wrote:
It works if I create the object, assign it to reference variable, and then use that reference variable to invoke bark() -

Hound h = new Hound();
h.bark();

but why doesn't it work if I just create and provide an instance of Hound on which I want to run bark() on ?


You mean new Hound().bark()? If the above works, with the "h" variable, then this will also work. If you're saying it doesn't, then you're doing something else different.
Vaibhav B Sharma
Greenhorn

Joined: Nov 28, 2011
Posts: 7

That was a silly, silly error on my part !!

Thanks for your inputs.. Have a nce weeked !
 
Don't get me started about those stupid light bulbs.
 
subject: Example for Certification book doesnt work
 
Similar Threads
To which class, object is refering to?
Reference Variable casting ... compile Errors vs Runtime errors
Problem with casting
Please clarify my understanding of Polymorphism
Confusion in Polymorhphism Self test questions K&B Book