• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with the output  RSS feed

 
Manish Bilung
Greenhorn
Posts: 13
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

I need help regarding the below code:



When I run the above code, I get the below error:
Animal.java:9: error: cannot find symbol
System.out.println("Harry Potter went to Hogwarts with "+ name);
                                                          ^
  symbol:   variable name
  location: class Animal
Animal.java:15: error: cannot find symbol
System.out.println(ref.name);
                      ^
  symbol:   variable name
  location: variable ref of type Animal
2 errors

My doubts:
1. I have written all the variable/field values inside the class
2. does the { } masks the String name ?

Thanks and Regards,
mbilung

 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Bilung wrote:
2. does the { } masks the String name ?


Variable declarations within blocks are local variables, and is only in scope within that block.

Henry
 
Manish Bilung
Greenhorn
Posts: 13
Chrome Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Manish Bilung wrote:
2. does the { } masks the String name ?


Variable declarations within blocks are local variables, and is only in scope within that block.

Henry


Thank you very much Henry !
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Dor Burd
Greenhorn
Posts: 19
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll throw in some extra information for you.

Those {} outside the method are called initialisation blocks. They run at the beginning of a constructor when a class is instantiated. If you declare your class variable, you can assign it in your init block:





 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this topic you'll find an excellent explanation (with illustrative code snippets) about the order of initialization blocks (both static and instance).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!