September 21, 2007

Static modifier

Selain access modifier (public, private & protected) terdapat pula beberapa jenis modifier lain di Java. Modifier-modifier tersebut antara lain adalah static

Bila kita meambahkan keyword static pada variable, maka variable tersebut akan tetap menempati sebuah alamat di memory, tak peduli seberapa banyak kita menginstansi / membuat object baru.
Contoh:

public class TheClass {
private static int theVariable;

public void setTheVariable(int newVariable){ //method untuk megubah nilai theVariable
TheClass.theVariable = newVariable;
}

public int getTheVariable(){ //method untuk menampilkan nilai the Variable
return(theVariable);
}
}


Kemudian kita membuat 2 buah instance class TheClass
TheClass myVariable = new TheClass();
TheClass yourVariable = new TheClass();

Dan apablia myVariable mengubah variable theVariable, maka bila yourVariable menampilkan theVariable, nilai yang tampil adalah nilai yang di set oleh myVariable
myVariable.setTheVariable(5); //myVariable mengubah nilai theVariable

yourVariable.getTheVariable; //yourVariable menampilkan theVariable
//maka yang tampil adalah 5


Bila keyword static ditambahkan pada method, maka hak akses method tersebut akan berubah
- Non-Static method bisa mengakses static variable & static method secara langsung
- Non-Static method bisa mengakses non-static variable & non-static method secara langsung
- Static method bisa mengakses static variable & static method secara langsung
- Static method tidak bisa mengakses non-static variable & non-static variable secara langsung

Additional :
Modifier static bila diterapkan pada method/variable, maka akan membuat method/variable tersebut bisa dipanggil secara langsung tanpa perlu meng-instance class-nya

Contohnya:
public class MyClass {
private static String statVar = "I'm a class variables!";
private String thevar = "I'm a instance variables!";

public static String statMet () {
return "I'm a class mathod!!";
}

public String theMet(){
return "I'm a instance method!!"
}
}

Sehingga kita bisa mengakses statVar secara langsung dengan cara:

MyClass.statVar
Atau mengakses statMet langsung dengan cara :

MyClass.statMet()
Tapi untuk mengakses theVar / theMet, maka kita harus meng-instance MyClass terlebih dahulu

public class AnotherClass{
private MyClass anotherVar = new MyClass();
}

Jadi bila ingin mengakses theVar / theMet, caranya adalah:
anotherVar.theVar;
atau
anotherVar.theMet();

13 comments:

Anonymous said...

Hi there! This blog post could not be written much better!

Looking through this post reminds me of my previous roommate!
He constantly kept preaching about this. I most certainly will forward this post to him.
Fairly certain he will have a very good read. I appreciate you for sharing!



my homepage ... Low Carb Ernährung Erfahrungen

Anonymous said...

What's up everybody, here every person is sharing these kinds of familiarity, therefore it's
good to read this webpage, and I used to go to
see this blog everyday.

Here is my web site: bilanzbuchhalter ihk hamburg

Anonymous said...

I visited several web sites but the audio quality
for audio songs present at this web page is
in fact fabulous.

My web blog :: hq lenkmatten 4 leiner

Anonymous said...

Great post! We will be linking to this particularly great
article on our website. Keep up the good writing.


Here is my web page; herrenröcke

Anonymous said...

Hi to every body, it's my first go to see of this blog; this web site contains awesome and truly good stuff in favor of visitors.

My blog: glutenfreie Lebensform

Anonymous said...

Great post. I was checking constantly this blog and I'm impressed! Extremely useful info particularly the last part :) I care for such information much. I was seeking this certain information for a very long time. Thank you and best of luck.

My weblog: aminosäuren zum abnehmen

Anonymous said...

We stumbled over here different page and thought I should check things out.
I like what I see so i am just following you.
Look forward to looking at your web page repeatedly.

Feel free to surf to my webpage: design wordpress theme

Anonymous said...

Awesome article.

Take a look at my web page; wordpress grundkurs
My website: professional wordpress templates

Anonymous said...

Good post. I learn something totally new and challenging on blogs I stumbleupon everyday.
It's always useful to read through articles from other authors and practice something from other sites.

Feel free to surf to my homepage bilanzbuchhalter ihk frankfurt

musik klasik said...

makasih info nya,.

Obat Penghancur Lemak Dalam Darah Tradisional said...

terimakasih untuk informasinya,ini sangat memberikan kami sebuah pengetahuan dan wawasan baru yang sebelumnya tidak mengetahuinya

Obat Penghilang Benjolan Di Payudara Tradisional said...

pekerjaan yang baik gan, artikel ini sangat menarik untuk dicatat, keren deh,, tentu saja kita memiliki wawasan baru yang kita dapatkan setelah membaca itu, yah thanx

Obat Pembesar Penis Titan Gel asli said...

JUAL OBAT KUAT PRIA DAN PEMBESAR PENIS HASIL PERMANEN DAN MANTAP. YUK CEKK...