Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Variable in java

 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have two doubt.

one if have one method and inside that mathod i have one variable can we use method variable without intialize it?

and second can method use class variable without intialize it value?

wat happen in both cases?


Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use variables that have not been initialized. If you try, the compiler will give you an error message.

However, class member variables are initialized implicitly if you don't initialize them yourself, whereas local variables (variables inside methods) are not - they must be initialized explicitly. For example:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the best way to learn this stuff is to try it out!!! write small, simple programs that do almost nothing (a "Hello, world!!" program would work for many cases), and see what happens!!! it's fast, easy, and you will learn it faster and remember it better if you do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic