*************************************************************************************************************
调试这段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;
}  }
}

解决方案 »

  1.   

    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;
    } }
    }
      

  2.   

    把class Product 放到 class ObjTest的外面
      

  3.   

    你是在jdk几运行的?
    你这段代码,我在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;
    } }
    }
      

  4.   

    new ObjTest().new Product
      

  5.   

    楼主把把Product类的定义放在了类ObjTest中,当然出错.
    你要不然用: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;
    }}
      

  6.   

    同意六楼!
    Java高级群:224651178 有兴趣加一下