|
-
Dec 7th, 2005, 08:23 PM
#1
Thread Starter
Hyperactive Member
Static Fields Confusion
I'm still fuzzy on the topic of static variables in OOP. Correct me if I'm wrong or add to it but if you declare a static variable in a class, that variable is visible only to that class no matter what? I don't really understand. Any help would be great!
-
Dec 8th, 2005, 02:46 AM
#2
Re: Static Fields Confusion
A static var (attribute) in Java is a value shared by all instances. if you change it form an instance of the class it'll change for all other instances also.
A static var (within a method) value is preserved after exiting the method unlike regular vars
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Dec 8th, 2005, 01:09 PM
#3
Thread Starter
Hyperactive Member
Re: Static Fields Confusion
So if I instantiate a class called Book three times and I have a static variable called bookISBN and I change the value of that variables through a method, that value will then change for all three instances right? And static variables within a method would basically be just like constants right?
So then a static method would be a method shared by all instances of that class right?
-
Dec 8th, 2005, 03:28 PM
#4
Re: Static Fields Confusion
Right except that a static value in a method can be modified but doesn't lose value upon exiting method
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Dec 11th, 2005, 03:19 AM
#5
New Member
Re: Static Fields Confusion
In OOP terms a static member or method does not require an instance of that object. To draw a real world comparison; think of a plastic mould used to create chocolate Santa's. This mould is the class/template used to create many instances of Chocolate Santa's.
On the mould however, there is a batch number. Every time you create new Santa, the batch number appears on the bottom and it does not change, however one can still read the batch number without actually creating the Santa, simply by looking at it.
In OOP terms you can access a static member or method using the class name:
Code:
class ChocolateSanta
{
private static int BATCH_NUMBER=1234;
private static int productionCount = 0;
private boolean melted = false;
public ChocolateSanta() {
productionCount++;
}
public int static getBatchNumber() {
return BATCH_NUMBER;
}
public static int getProductionCount() {
return productionCount;
}
public void melt()
{
melted = true;
}
public boolean getMelted()
{
return melted;
}
}
public class StantaFactory
{
public static void main(String[] args)
{
System.out.println(ChocolateSanta.getProductionCount() + " Santa's made");
ChocolateSanta yumYum = new ChocolateSanta;
System.out.println(ChocolateSanta.getProductionCount() + " Santa's made");
yumYum.melt();
if (yumYum.getmelted()) {
System.out.println("Sorry your chocolate santa has melted");
}
}
}
You can see that another static variable is used to keep track of the production count. And, static methods are used to access the static variables. Remember that one can only access static class variables and methods inside a static method.
You can also declare an entire class static. If you do this, you can never create an instance of it. This can be useful in a situation where you may want to create a library of global methods for example.
I am the real Santa Clause 
-
Dec 11th, 2005, 07:07 AM
#6
Re: Static Fields Confusion
You do know Santa is a myth, don't you?
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Dec 11th, 2005, 05:12 PM
#7
Hyperactive Member
Re: Static Fields Confusion
 Originally Posted by ComputerJy
You do know Santa is a myth, don't you?
Santa, is that you?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|