---
title: "java创建对象的两种方法-写给自己的博客"
author: "chinagdszsuby"
date: "2010-07-25T14:49:48Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2010-07-25T20:50:46Z"
id: 720796
url: "https://sby.iteye.com/blog/720796"
---

```java
class OtherClass 
{ 
	int i = 0; 

	OtherClass() 
	{ 
		i ++; 
	} 
} 

public class TestOther 
{ 
	public static void main(String[] args) throws Exception 
	{ 
		//方法一
		OtherClass o = new OtherClass(); 
		System.out.println("o.i = " + o.i); 

		//方法二
		Class c = Class.forName("OtherClass"); 
		OtherClass t = (OtherClass)c.newInstance(); 
		System.out.println("t.i = " + t.i); 
	} 
}
```