Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Static and Final

Static

Ok so everything is done in classes in Java, we create instances of classes with the new keyword and can pass classes down to other classes as parameters or create classes within classes thats all well and good but what if you want a function or variable which is accessible from absolutly anywhere within the program and retains its value? In BASIC you would call this a global variable or a public variable within a module, in Java we don't have modules or global.

The static statement is added before declaring a variable or method, and meens that variable or method is always the same through every instance of the class it was used in. What?
Start a new project call it statictest with the main class.

Code:
public class statictest {

public static void main(String[] args) {

// TODO Auto-generated method stub
THETEST test_class_1 = new THETEST();
THETEST test_class_2 = new THETEST();

System.out.println("Initial value 1: "+test_class_1.static_string);
System.out.println("Initial value 2: "+test_class_2.static_string);

test_class_1.static_string = "'here it is'";

System.out.println("Set it on 1: "+test_class_1.static_string);
System.out.println("Recieve it here: "+test_class_2.static_string);

System.out.println("As you can see the value is always the same for all class instances!");
}
}
Add a class "THETEST"
Code:
public class THETEST {

public static String static_string="";
}
And run it don't worry about the warnings :-)
You will see the value is the same on both instances, so what does this meen for functions, well first lets get rid of those warnings and you will see. Hovering over the yellow text shows "The STATICTEST.static_string should be accessed in static way", ok if you think about it, if a variable is the same accross ALL instances of a class then why should you even bother creating or calling a single instance of that class? it wastes time, resources and makes your code confusing. There has got to be another way. test_class_1 is an instance of THETEST, calling test_class_1.static_string creates a warning, so simply do THETEST.static_string, that way your calling the object itself and no instances of the object, and the warning is gone! You can now get and set static_string without creating any instances of the class.
So for functions it maybe useful to have a global function 'add' you don't want to carry a pesky instance of the class say math around all over the program do you?
Add this code to THETEST:
Code:
public class THETEST {

public static String static_string="";

public static int add(int num1, int num2){

return num1+num2;
}
}
// you may call this new function by:
System.out.println(THETEST.add(1, 3));
Notice:
1. STATIC functions can only call other static functions
2. STATIC functions can only read/write to other static variables
3. STATIC variables can hold instances of objects
4. A STATIC function may call a non-static function from static variables holding object

Final

Slightly easier, and by slightly easier I meen ALOT easier, FINAL is the same as constant in basic, its meant to be used to stop a variable or function (remember functions maybe overloaded) from being changed, also when programming mobile applications it inherits a slight performance boost.

Code:
public class THETEST {

public static final int number_1=1;
}
And to test the theory
Code:
public class statictest {

public static void main(String[] args) {

THETEST.number_1 = 2;
}
}
Eclipse instantly marks this code as an error number_1 cannot be written to it is final!

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source