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

variables

Becky Miller
Greenhorn

Joined: Jan 24, 2001
Posts: 17
Hi, I was doing a tutorial and was very confused with all these terms that came up. Can some one explain what member variables, instance variables, local variables are.
what the differences are?
Rakesh Sharma
Ranch Hand

Joined: Dec 29, 2000
Posts: 37
Dear Becky
Member variable & Instance variables are the same thing. These are the variables which are declared in the class and used for the creation of object.
Local varables are the varables defined inside a method. These are used inside that particular method only and cannot be accessed outside that methods.
Hope this clarifies.
Becky Miller
Greenhorn

Joined: Jan 24, 2001
Posts: 17
Thanxs, Rakesh. Those terms are much clearer now.
When you create an class, how do you know whether to declare a variable as public or simply w/o any access modifiers.
For example : public int width = 0;
public int height = 0;
public Point origin;
These variables are all declared with the public keyword. But there are programs that just declare a variable as simply
int x =1;
double y =0;
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I depends on what you need to do with the variables.
If you want other classes to be able to modify the variables directly, you probably want to declare them public.
If they are used just in the class, you can either not have an access modifier, or declare them private.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
public = accessible from any class, even classes in other packages
private = accessible only from within the same class
default (no modifier) = accessible from any class in the same package
If you don't specifically say that any of your classes are in a different package, then you can use the default (no modifier) declaration, since all your classes would then be in the same package, known as the default package.
~ Ryan


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
default (no modifier) = accessible from any class in the same package
isn't default also seen in the same package but to class subclasses which located in that package no where else ? just trying to check if i know my packages or not.


Val SCJP <BR>going for SCJD
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
default is seen from the same class, from any class in the same package, and from any subclass in the same package.
~Ryan
Leslie Gill
Greenhorn

Joined: Feb 01, 2001
Posts: 3
Is it possible to do the following:
1. declare a static variable inside a method
2. have the method return the value of this variable
3. use this method in the main method of the same class to assign the result to a variable
I am getting an error when compiling. Error states that static is "illegal start of expression". If I remove the word static, it compiles and runs without error. I just don't completely understand static variables, and I'm having a hard time finding resources to explain it properly.
Thank you for your help!
Originally posted by Rakesh Sharma:
Dear Becky
Member variable & Instance variables are the same thing. These are the variables which are declared in the class and used for the creation of object.
Local varables are the varables defined inside a method. These are used inside that particular method only and cannot be accessed outside that methods.
Hope this clarifies.


Leslie
Jan Volkmann
Greenhorn

Joined: Feb 01, 2001
Posts: 17
Originally posted by Rakesh Sharma:

Member variable & Instance variables are the same thing. These are the variables which are declared in the class and used for the creation of object.
Local varables are the varables defined inside a method. These are used inside that particular method only and cannot be accessed outside that methods.

Hi Rakesh and Leslie,
Instance variables are Member variables but the opposite needs NOT to be true. In short: Varaibles declared or defined within a class but outside a method are member varaibles (member of a class) otherwise they are local variables (declared or defined within a method). This is the basic distinction (Unlike in C++ varaibles canNOT be declared or defined outside a class). Member variables are further subdivided into instance variables and class variables. Neither is used to create an object. The difference is just that instance vars are attached to a distinct object (i.e. created with it and destroyed with it) whereas class variables are attached to the class itself (i.e. created only once at start of program and not destroyed until the program is finished).
How to create a class variable? Easy: static <type> <name> = <value>;
How to adress them? this.<name>=<value>; or just <name>=<value>
For obvious reasons it's not allowed to define class vars witjhin a method.
The same holds true for class methods (i.e. static methods).
Hope it helps,
Jan
Jan Volkmann
Greenhorn

Joined: Feb 01, 2001
Posts: 17
oops, used the wrong kind of brackets. it was supposed to look like this:
static (type) (name) = (value); //class var definition
if accessed from outside (if possible) call it like this:
(classname).(variablename)
if accessed from within its class refer to it like this:
this.(variablename)
or:
(variablename)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables
 
Similar Threads
scripting variables
answer this
using Interface reference for objects
Volatile
Static variables?