public class StaticVariableTest {
public static void main(String []args){
Student s1 = new Student();
Student.height =1;
Student s2 = new Student();
Student.height =2;
System.out.println(Student.height);
}
}
class Student{
static int height;
}
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2012年3月26日 星期一
Java Static Variable
Java ArrayList
public class ArrayListDemo {
public static void main(String[] args) {
/**
* Following example will cause a run time exception:
* java.lang.ClassCastException, although no compilation error happened.
*/
/*
* int sum = 0;
* ArrayList al = new ArrayList();
* al.add("1");
* al.add(new Integer(2));
* for (int i = 0; i < al.size(); i++) {
* sum+= (Integer)al.get(i);
* }
* System.out.println(sum);
*/
/**
* Following example will fix above problem.
*/
int sum = 0;
ArrayList al = new ArrayList();
// al.add("1"); this line has a compilation error.
al.add(new Integer(1));
al.add(new Integer(2));
for (int i = 0; i < al.size(); i++) {
sum += (Integer) al.get(i);
}
System.out.println(sum);
}
}
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)