File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic OOD question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic OOD question" Watch "Basic OOD question" New topic
Author

Basic OOD question

Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
Hey again all,

I have a basic OOD question...

Where is the best place to declare your instance variables? I've read somewhere that it's best to declare and initalize them inside of your constructor method. Did I read this wrong? Because, after thinking about it, wouldn't they be out of scope to the rest of the class if they're declared inside the constructor? They would just be considered local variables to the constructor method wouldn't they? I thought instance variables were declared inside of a class but outside of any method, right? Am I getting declaration and initalization mixed-up?

For example:

#1:


or is it supposed to be more like this one:

#2:


Another question, what language was Java written in, machine language?
[ July 16, 2006: Message edited by: Brian LaRue ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Brian LaRue:
Hey again all,

I have a basic OOD question...

Where is the best place to declare your instance variables? I've read somewhere that it's best to declare and initalize them inside of your constructor method. Did I read this wrong? Because, after thinking about it, wouldn't they be out of scope to the rest of the class if they're declared inside the constructor? They would just be considered local variables to the constructor method wouldn't they? I thought instance variables were declared inside of a class but outside of any method, right? Am I getting declaration and initalization mixed-up?

For example:

#1:


or is it supposed to be more like this one:

#2:


Another question, what language was Java written in, machine language?


Well according to your code you are not using instance variable. You are using class level variables, and if you want to know where it should be declared,

Class Variables:
A class variable should be declared in starting of the class code. but it should be defined or initialized into constructer, although if you don't initialize the class members it will initialized by default value of that type. and if you want to explicitly initialize them then you may use static block to initialze them, which will initialize them before the constructer is called.

Now come to Instance Variables:
These are the variable which are visible into local scope that means that you are declaring these into method level. Now instance variables should be initialized before they are in use, otherwise they will throw a compile time exception. It is suggested for instance variables that use it where they are required only, i.e. if you need a variable in a loop only then there is no need to use at method level. You can declare and initiliaze them in the loop only so that they will become out of scope after and before the loop.

Hope this helps you.

Now about your second question

JAVA is written in two languages

1. C (Native Code)
2. Itself in JAVA.

Pls correct me if I am wrong anywhere.


The Best way to predict your future is to create it - Every great individual common man
Brian LaRue
Ranch Hand

Joined: Feb 01, 2006
Posts: 45
ok then I guess I'm confused as to what an instance variable is.

I thought an instance variable is a variable that was contained inside an object on the heap. They were declared inside a class but outside of any method. Its primary use is to keep track of an objects state, right? I believed that each object, once instantiated, had it's own copy of this variable and each could have its own value. Aren't these the variables that have access modifiers that relate to them (public, private, protected, static, and final)? I was under the impression that the instance variables were, if not alreay initalized, given a default value (0 for int, 0.0 for float, false for boolean, and null for reference variables) when each object is instantiated.--I'm confused--

Local variables i believed to only be alive while on the stack, and only in scope while their method is executing. I thought these to not have access modifiers because they only were alive when their method had been called and were more 'temporary'. Can an outside class access and modify a local variable? That doesn't sound like it could happen! I knew these had to be initalized because it would throw a compiler error since you couldn't modify them without having a beginning state.

From the following example, do I not have it right? Or is it supposed to be reversed (instance/local variables)?

Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
"Class" variables are those variables of a class that should be the same across all instances and/or threads (in otherwords static and maybe static final). Instance variable are also declared in the class, but are not static, and so their values are "local" to the instance in which they are defined. Your number 2 example was correct.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ankur Sharma:
Now come to Instance Variables:
These are the variable which are visible into local scope that means that you are declaring these into method level.


Those are *local* variables. *Instance* variables are in fact non-static fields - what you called class variables.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic OOD question