*************************************************************************************************************
调试这段java代码是出错: 无法从静态上下文中引用非静态 变量 this
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
^
1 错误处理已完成。请问这是为什么?怎么改?谢谢大家!
*************************************************************************************************************
import java.io.*;
import java.util.*;public class ObjTest
{
public static void main(String[] args)
{
try
{
FileOutputStream fs=new FileOutputStream("product.obj");
ObjectOutputStream os=new ObjectOutputStream(fs);
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
os.writeObject(p);
os.flush();
fs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
class Product implements java.io.Serializable
{
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript;
public Product(String ProductNo,String ProductName,String ProductClass,String ProductType,int ProductNumber,
int MinNumber,Double ProductPrice,String ProductArea,String SupplierCompany,String ProductDescript)
{
this.ProductNo=ProductNo;
this.ProductName=ProductName;
this.ProductClass=ProductClass;
this.ProductType=ProductType;
this.ProductNumber=ProductNumber;
this.MinNumber=MinNumber;
this.ProductPrice=ProductPrice;
this.ProductArea=ProductArea;
this.SupplierCompany=SupplierCompany;
this.ProductDescript=ProductDescript;
} }
}
调试这段java代码是出错: 无法从静态上下文中引用非静态 变量 this
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
^
1 错误处理已完成。请问这是为什么?怎么改?谢谢大家!
*************************************************************************************************************
import java.io.*;
import java.util.*;public class ObjTest
{
public static void main(String[] args)
{
try
{
FileOutputStream fs=new FileOutputStream("product.obj");
ObjectOutputStream os=new ObjectOutputStream(fs);
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
os.writeObject(p);
os.flush();
fs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
class Product implements java.io.Serializable
{
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript;
public Product(String ProductNo,String ProductName,String ProductClass,String ProductType,int ProductNumber,
int MinNumber,Double ProductPrice,String ProductArea,String SupplierCompany,String ProductDescript)
{
this.ProductNo=ProductNo;
this.ProductName=ProductName;
this.ProductClass=ProductClass;
this.ProductType=ProductType;
this.ProductNumber=ProductNumber;
this.MinNumber=MinNumber;
this.ProductPrice=ProductPrice;
this.ProductArea=ProductArea;
this.SupplierCompany=SupplierCompany;
this.ProductDescript=ProductDescript;
} }
}
解决方案 »
- AnimatedGifEncoder 生成的图片背景颜色与图标背景颜色如何设置透明
- 刚用spring3.0+hibernate3.x+struts2.x搭建的,结果无法正常启动,求指点
- struts和hibernate的注册 帮忙修改一下 可以不
- hibernate dialect 问题
- 新手求救 。。。
- java里有什么规范 jsp里有什么规范
- 在jb9中如何切换tomcat?
- 高手进来看看,关于EJB插入数据库的TransactionRolledbackLocalException
- 散分散分!这鸟JBUILDER终于搞定了
- jpql语句和sql语句问题
- hibernate奇怪的问题?
- 关于Struts2配置动态结果的问题。。
或者
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;public class ObjTest {
public static void main(String[] args) {
init();
}
public static void init(){
try {
FileOutputStream fs = new FileOutputStream("product.obj");
ObjectOutputStream os = new ObjectOutputStream(fs); Product p = new Product("X123", "戴尔", "计算机", "L321", 25, 15,
3500.0, "广州", "电脑城", "计算机设备");
os.writeObject(p);
os.flush();
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
} static class Product implements java.io.Serializable {
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript; public Product(String ProductNo, String ProductName,
String ProductClass, String ProductType, int ProductNumber,
int MinNumber, Double ProductPrice, String ProductArea,
String SupplierCompany, String ProductDescript) {
this.ProductNo = ProductNo;
this.ProductName = ProductName;
this.ProductClass = ProductClass;
this.ProductType = ProductType;
this.ProductNumber = ProductNumber;
this.MinNumber = MinNumber;
this.ProductPrice = ProductPrice;
this.ProductArea = ProductArea;
this.SupplierCompany = SupplierCompany;
this.ProductDescript = ProductDescript;
} }
}
你这段代码,我在jdk 6编译都有问题。
import java.io.*;
import java.util.*;public class ObjTest implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L; public static void main(String[] args) {
try {
FileOutputStream fs = new FileOutputStream("product.obj");
ObjectOutputStream os = new ObjectOutputStream(fs);
Product p = new ObjTest().new Product("X123", "戴尔", "计算机", "L321", 25, 15,3500.0, "广州", "电脑城", "计算机设备");
os.writeObject(p);
os.flush();
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
} class Product implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript; public Product(String ProductNo, String ProductName,
String ProductClass, String ProductType, int ProductNumber,
int MinNumber, Double ProductPrice, String ProductArea,
String SupplierCompany, String ProductDescript) {
this.ProductNo = ProductNo;
this.ProductName = ProductName;
this.ProductClass = ProductClass;
this.ProductType = ProductType;
this.ProductNumber = ProductNumber;
this.MinNumber = MinNumber;
this.ProductPrice = ProductPrice;
this.ProductArea = ProductArea;
this.SupplierCompany = SupplierCompany;
this.ProductDescript = ProductDescript;
} }
}
你要不然用:Product p=new ObjTest().new Product(...)
要不然把Product类的定义放在ObjTest外面 ,如下
import java.io.*;
import java.util.*;public class ObjTest {
public static void main(String[] args) {
try {
FileOutputStream fs = new FileOutputStream("product.obj");
ObjectOutputStream os = new ObjectOutputStream(fs); Product p = new Product("X123", "戴尔", "计算机", "L321", 25, 15,
3500.0, "广州", "电脑城", "计算机设备");
os.writeObject(p);
os.flush();
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
}}class Product implements java.io.Serializable {
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript; public Product(String ProductNo, String ProductName, String ProductClass,
String ProductType, int ProductNumber, int MinNumber,
Double ProductPrice, String ProductArea, String SupplierCompany,
String ProductDescript) {
this.ProductNo = ProductNo;
this.ProductName = ProductName;
this.ProductClass = ProductClass;
this.ProductType = ProductType;
this.ProductNumber = ProductNumber;
this.MinNumber = MinNumber;
this.ProductPrice = ProductPrice;
this.ProductArea = ProductArea;
this.SupplierCompany = SupplierCompany;
this.ProductDescript = ProductDescript;
}}
Java高级群:224651178 有兴趣加一下