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.statVarAtau 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:
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
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
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
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
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
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
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
Awesome article.
Take a look at my web page; wordpress grundkurs
My website: professional wordpress templates
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
makasih info nya,.
terimakasih untuk informasinya,ini sangat memberikan kami sebuah pengetahuan dan wawasan baru yang sebelumnya tidak mengetahuinya
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
JUAL OBAT KUAT PRIA DAN PEMBESAR PENIS HASIL PERMANEN DAN MANTAP. YUK CEKK...
Post a Comment