Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instance Variables

 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you create them in java?
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the stuff on this page:
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you declare a variable without the keyword "static," you have an instance variable.

This is all explained in Ko's link above, but let me try my own brief explanation...

You define Java classes, which (usually) include fields (variables) and methods (functions).

The class can serve as a "blueprint" for creating an object, also known as an "instance" of the class (which you create using the keyword "new"). By default, fields and methods are "instance" members, meaning that an instance must be created in order for these fields and methods to be used.

In contrast, fields and methods can be declared "static," in which case they are not associated with any particular instance, but are instead simply associated with the class itself. To use static fields and methods, no instance needs to be created -- they can be called directly from the class.

[ November 12, 2005: Message edited by: marc weber ]
 
armeen golden
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so can we say that instance varialbes are associated with a particular instance and not with the entire class?
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic