无法从静态上下文中引用非静态 变量 this
*************************************************************************************************************
调试这段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;
}
}
}
[解决办法]
static class Product implements java.io.Serializable
或者
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;
}
}
}
[解决办法]
把class Product 放到 class ObjTest的外面
[解决办法]
你是在jdk几运行的?
你这段代码,我在jdk 6编译都有问题。
- Java code
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 = [color=#FF0000]new ObjTest().new Product[/color]("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; } }}
[解决办法]