JavaÖ®staticѧϰ֪ʶ½éÉÜ
Java֮staticѧϰ
¶ÔÓÚѧϰstaticÎÒÃÇÏÈÀ´¿´Á½¸öÀý×Ó£º
//Example 1 Tst.java£¬no main() method
package com.blogchina.qb2049;
public class Tst
{
static { System.out.println("111111"); }
}
ÔËÐнá¹ûΪ: 111111
Exception in thread "main" java.lang.NoSuchMethodError: main
ͬÑùµÄµÀÀí¿´µÚ¶þ¸öÀý×Ó£º
//Example 2 TFrame.java,no main() method
package com.blogchina.qb2049;
import javax.swing.JFrame;
public class TFrame
{
static {
JFrame jfr_fr= new JFrame("ÎÞmain£¨£©
jfr_fr.setLocation(400,300);
jfr_fr.setSize(200,300);
jfr_fr.setVisible(true);
jfr_fr.setDefaultCloseOperation(jfr_fr.EXIT_ON_CLOSE);
}
}ÔËÐнá¹ûÈçÏÂͼ£º
Õâµ½µ×Ôõô
ѧϰ±¾ÎÄÄãµ½µ×Ҫѧµ½Ê²Ã´£º
1¡¢staticÔÚjavaÖе½µ×´ú±íʲô£¬ÎªºÎÒªÓÃËü£¿
2¡¢staticÔÚjavaÖÐÔõôÓã¿
3¡¢static ÓÐÄÇЩÌصãºÍʹÓõē¾ÖÏÞ”£¿
1¡¢staticÔÚjavaÖе½µ×´ú±íʲô£¬ÎªºÎÒªÓÃËü£¿
static¨D¨D¾²Ì¬¨D¨D“Ö¸¶¨Î»ÖÓ
Ê×ÏÈ£¬ÎÒÃÇÀ´¿´¿´javaµÄÄڴ棺java°ÑÄÚ´æ·ÖΪջÄÚ´æºÍ¶ÑÄڴ棬ջÄÚ´æÓÃÀ´´æ·ÅһЩ»ù±¾ÀàÐ͵ıäÁ¿ºÍÊý×é¼°¶ÔÏóµÄÒýÓñäÁ¿£¬¶ø¶ÑÄÚ´æÖ÷ÒªÊÇÀ´·ÅÖöÔÏóµÄ¡£
ÓÃstaticµÄÐÞÊεıäÁ¿ºÍ·½·¨£¬Êµ¼ÊÉÏÊÇÖ¸¶¨ÁËÕâЩ±äÁ¿ºÍ·½·¨ÔÚÄÚ´æÖеē¹Ì¶¨Î»ÖÔ£static storage¡£¼ÈȻҪÓГ¹Ì¶¨Î»ÖÔÄÇôËûÃǵē´óС”Ëƺõ¾ÍÊǹ̶¨µÄÁË£¬ÓÐÁ˹̶¨Î»Öú͹̶¨´óСµÄÌØÕ÷ÁË£¬ÔÚÕ»Öлò¶ÑÖпª±Ù¿Õ¼äÄǾÍÊǷdz£µÄ·½±ãÁË¡£Èç¹û¾²Ì¬µÄ±äÁ¿»ò·½·¨ÔÚ²»³öÆä×÷ÓÃÓòµÄÇé¿öÏ£¬ÆäÒýÓþä±úÊDz»»á·¢Éú¸Ä±äµÄ¡£
ÎÒÃdz£¿´µ½£ºstatic±äÁ¿ÓеãÀàËÆÓÚCÖеÄÈ«¾Ö±äÁ¿µÄ¸ÅÄ¾²Ì¬±íʾµÄÊÇÄÚ´æµÄ¹²Ïí£¬¾ÍÊÇËüµÄÿһ¸öʵÀý¶¼Ö¸Ïòͬһ¸öÄÚ´æµØÖ·¡£°ÑstaticÄÃÀ´£¬¾ÍÊǸæËßJVMËüÊǾ²Ì¬µÄ£¬ËüµÄÒýÓ㨺¬¼ä½ÓÒýÓ㩶¼ÊÇÖ¸Ïòͬһ¸öλÖã¬ÔÚÄǸöµØ·½£¬Äã°ÑËü¸ÄÁË£¬Ëü¾Í²»»á±ä³ÉÔÑù£¬Äã°ÑËüÇåÀíÁË£¬Ëü¾Í²»»á»ØÀ´ÁË¡£ÎÒÃdz£¿É¿´µ½ÀàËÆÒÔϵÄÀý×ÓÀ´ËµÃ÷Õâ¸öÎÊÌ⣺
class Student{
static int numberOfStudents=0;
Student()
{
numberOfStudents++;
}
}ÿһ´Î´´½¨Ò»¸öеÄStudentʵÀýʱ,³ÉÔ±numberOfStudents¶¼»á²»¶ÏµÄµÝÔö,²¢ÇÒËùÓеÄStudentʵÀý¶¼·ÃÎÊͬһ¸önumberOfStudents±äÁ¿,ʵ¼ÊÉÏint numberOfStudents±äÁ¿ÔÚÄÚ´æÖÐÖ»´æ´¢ÔÚÒ»¸öλÖÃÉÏ¡£ÎªÁËÀ´¸üºÃµÄ˵Ã÷Õâ¸öÎÊÌ⣬ÎÒÌØÒâѧϰÕÅÀÏʦ£¨ÕÅТÏ飩ʹÓÃÒ»¸öÐÎÏóµÄͼ½â£º
´´½¨Á½¸öStudentʵÀý£¬stu1£½new Student(); stu2=new Student();
¶à¸öʵÀý¹²ÏíÒ»¸ö±äÁ¿Ëƺõ²»×ãÒÔÈÃÎÒÃǶÔstaticÄÇôµÄÈÈÇ飬ʵ¼ÊÉÏjavaÒýÈëstaticÈ´ÓÐÁíÍâµÄº¬Ò壺
(1)¡¢ÒýÓÃstaticµÄ·½·¨ºÍ±äÁ¿£¬²»ÐèÒªºÍʵÀýÀ¦°óÔÚÒ»Æð,Õâ¿ÉÒÔÌá¸ß´úÂëµÄ±àдµÄЧÂÊ£¬ÕâÑùµÄÀý×ÓÎÒÃÇËæ´¦¿É¼û£»
(2)¡¢javaµÄÖ÷ÀàÖÐmain()·½·¨±¾Éí¾ÍÊÇÒ»¸östaticµÄ£¬ËùÒÔmain·½·¨µÄÖ´ÐоÍÊÇÔÚûÓвúÉúеÄʵÀýµÄÇé¿ö£»¶ÔÓÚ¾²Ì¬µÄ¶«Î÷£¬JVMÔÚ¼ÓÔØÀàʱ£¬¾ÍÔڃȴæÖпª±ÙÁËÕâЩ¾²Ì¬µÄ¿Õ¼ä£¬ËùÒÔËäûÓо²Ì¬µÄmain()·½·¨µ«ÊdzÌÐò»¹ÊÇÖ´ÐÐÁË£¬²»¹ýÅ׳öÀ´ÁËÎÞmain()·½·¨µÄÒì³£¡£ÕâÒ²²»ÖªËã²»ËãÊÇjavaµÄÒ»¸ö©¶´£»
(3)¡¢Èç¹ûÐèÒª´´½¨Ò»¸öÍÑÀëÓÚʵÀýµÄ±äÁ¿»ò·½·¨£¨Ö»ÓëÕû¸öÀàÓйأ©£¬ÄÇôÓÃstatic×÷ÐÞÊÎÊÇÔٺò»¹ýÁË£¬ÈçÎÒÃǾ³£¿´µ½ÒªÍ³¼ÆʵÀýʵÏֵĸöÊý£¨Í¨³£µÄÀý×Ó¾ÍÊǼÆÊý£©¡£
(4)¡¢Ê¹ÓÃÒ»ÖÖ¾²Ì¬µÄ·½·¨µÄ±à³Ìͨ³£½Ð×ö·ÀÓù(defensive)±à³Ì,Ëü¿ÉÒÔÔÚAPI¹©Ó¦ÉÌͻȻÖжÏÖ§³ÖµÄÇé¿öϱ£»¤´úÂë
2¡¢staticÔÚjavaÖÐÔõôÓã¿
ʹÓÃstaticʱ£¬Òª¼Ç×ÅÎÒ²ûÊöµÄstatic´ú±íʲô¡£
staticʹÓ÷dz£µÄ¼òµ¥£¬Èç¹ûÒªÐÞÊÎÒ»¸ö¾²Ì¬¿éÖ»Ð裺staic {……..}¼´¿É£¨³£Óþ²Ì¬¿éÀ´³õʼ»¯Ò»Ð©±äÁ¿£©£» ¾²Ì¬·½·¨¾Í²ÎÕÕmain()µÄÐÎʽ£º·ÃÎʱêʶ static returnType method(…) {};¾²Ì¬±äÁ¿¾ÍÊÇ£ºstatic type fields£»
ÔÚʹÓþ²Ì¬µÄ·½·¨Ê±£¬¿ÉÒÔÖ±½ÓÓÃÀàÃûÀ´ÒýÓ㬲»ÐèÒª´´½¨ÊµÀý£¨µ±È»´´½¨ÊµÀýÒ²ÊÇ¿ÉÒԵģ©£¬ÀýÈ磬System.out,String.valueOf()µÈµÈ¡£
3¡¢static ÓÐÄÇЩÌصãºÍʹÓõē¾ÖÏÞ”£¿
´ÓÉÏÃæµÄ·ÖÎö¿ÉÖª£¬staticµÄ¶«Î÷ÔÚÀà¼ÓÔØʱ£¬¾Í·ÖÅäÁËÄÚ´æ¿Õ¼ä£¬¼´±àÒëʱ¾ÍΪÕâЩ³ÉÔ±±äÁ¿µÄʵÀý·ÖÅäÁ˿ռ䡣
ÄÇôÔÚstatic¿éÄںͷ½·¨ÌåÄÚ£¬ÎÒÃÇÄܸøËü·ÅÒ»¸öÔÚÄÚ´æÖл¹Ã»ÓÐ×ÅÂäµÄ±äÁ¿£¿ÏÔÈ»ÓëÎÒÃÇÏÈÇ°Ëù˵µÄÏà×ó¡£staticµÄ¶«Î÷£¬È˼ÒÊÇÔÚstatic storageÖÐÓГָ¶¨Î»Öӵģ¬Èç¹ûÎÒÃÇãȻµÄÔÚstaticµÄ×÷ÓÃÓòÖзÅÖÃÒ»¸öÆÕͨµÄ±äÁ¿£¬ÄÇô±àÒëʱJVM¾ÍºÁ²»¿ÍÆøµÄ¸øÄã¸öÒì³££º
non-static variable a cannot be referenced from a static context»ònon-static method Test() cannot be referenced from a static context£¨×¢£ºTest()ÊÇÎÒÊÔÑéʱµÄÒ»¸ö
¶ÔÓÚstaticµÄ³õʼ»¯ÎÊÌ⣬ÎÒÃÇ»¹ÊÇÖµµÃÌÖÂ۵ġ£ÏÖ¿´ÏÂÃæµÄÀý×Ó£º
//StaticInit show the static decorated initialization
package com.blogchina.qb2049;
public class StaticInit
{
static int i;
int a;
public StaticInit()
{
a=6;
System.out.println("a µÄ³õʼ»¯"+a);
}
public static void main(String[] args)
{
new StaticInit();
}
static
{
i=5;
System.out.println("i µÄ³õʼ»¯"+i);
}
}ÔËÐнá¹ûÈçÏ£ºi µÄ³õʼ»¯5
a µÄ³õʼ»¯6¾²Ì¬¿éµÄ³õʼ»¯ÒªÔçÓڷǾ²Ì¬µÄ£¬ÔÒò¾ÍÊÇÔÚÓÚÕâЩ¶«Î÷ÊÇÔÚÀà×°ÔØʱ¾Í¿ªÊ¼³õʼ»¯ÁË¡£
˵ÆðstaticµÄ“¾ÖÏÞ“£¬×ܽáÆðÀ´¾ÍÊÇ£ºÔÚstaticµÄ·½·¨ÖнöÄܹ»µ÷ÓÃÆäËûµÄstatic·½·¨ºÍstatic±äÁ¿£»ÔÚstatic·½·¨Öв»ÄÜÒÔÈκη½Ê½ÒýÓÃthis»òsuper£»static±äÁ¿ÔÚ¶¨Òåʱ±ØÐë½øÐгõʼ»¯£¬²¢ÇÒ³õʼ»¯µÄʱ¼äÔçÓڷǾ²Ì¬¡£»¹ÓÐÒ»¸ö¾ÖÏÞÎÒÐèÒª¾ßÌåµÄ˵Ã÷һϣ¬staticµÄ±äÁ¿µÄ³õʼ»¯½öÄÜÒ»´Î£¬ÈçÏÂÀý£º
//Static.java£¬ initialize only one
class T1
{
static int t=1;
T1(int b)
{
t=b;
}
}
public class Static
{
T1 t1=new T1(2);
T1 t2=new T1(3);
T1 t3=new T1(4);
public Static()
{
System.out.println("t1: "+t1.t);
System.out.println("t2: "+t2.t);
System.out.println("t3: "+t3.t);
}
public static void main(String args[])
{
new Static();
}
}
ÔËÐнá¹ûΪ: t1: 4
t2: 4
t3: 4
¸Ãstatic±äÁ¿Ö»ÊǽÓÊÜÁË×îºóÒ»´ÎµÄ³õʼ»¯.ʵ¼ÊÕ⻹ÊÇÎÒÃÇÏÈÇ°Ìáµ½µÄ¶à¸öʵÀý¹²ÏíÒ»¸ö¾²Ì¬µÄ±äÁ¿µÄÎÊÌâ¡£
×ÜÖ®£¬static¾ÍÊǸøÎÒÃÇÒ»¸öÕâÑùµÄÖҸ棺staticµÄ¶«Î÷ÔÚ±àÒëʱ£¬¾ÍÒÑÏòÄÚ´æÒªµ½ÁË´æÈ¡¿Õ¼äÁË¡£
±¾ÎĵØÖ·£ºhttp://www.45fan.com/dnjc/68853.html