473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I solve this java problem?

18
ERROR MESSAGE[non-static variables this cannot be referenced from a static contex ]
public static void main(String []args)
Expand|Select|Wrap|Line Numbers
  1.   Product p = new Product(110,"Books",15.00,2010,"Wiliams");
  2.   p.setproductId(110);
  3.   p.setproductName("Books");
  4.   p.setCost(15.00);
  5.   p.setyearOfPublication(2010);
  6.   p.setpublishingHouse("Wiliams");
  7.  
  8.  
  9.   int productId = p.getproductId();
  10.   String productName = p.getproductName();
  11.   double cost = p.getCost();
  12.   int yearOfPublication = p.getyearOfPublication();
  13.   String publishingHouse = p.getpublishingHouse();
  14.  
  15. }
  16. class Product
  17. {
  18.    private int productId;
  19.    private String productName;
  20.    private double cost;
  21.    private int yearOfPublication;
  22.    private String publishingHouse;
  23.  
  24.  
  25. public Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
  26. {
  27.    this.productId = productId;
  28.    this.productName = productName;
  29.    this.cost = cost;
  30.    this.yearOfPublication = yearOfPublication;
  31.    this.publishingHouse = publishingHouse;
  32. }
  33.  
  34. public void setId(int productId)
  35.    {
  36.      this.productId = productId;
  37.    }
  38.  
  39. public int getproductId()
  40.    {
  41.        return productId;
  42.    }
  43. public void setproductName(String productName)
  44.    {
  45.      this.productName = productName;
  46.    }
  47.  
  48. public String getproductName()
  49.    {
  50.        return productName;
  51.    }
  52.  
  53.    public void setcost(double cost)
  54.    {
  55.      this.cost = cost;
  56.    }
  57.  
  58. public int getcost()
  59.    {
  60.        return cost;
  61.    }
  62.  
  63.    public void setyearOfPublication(int yearOfPublication)
  64.    {
  65.      this.yearOfPublication = yearOfPublication;
  66.    }
  67.  
  68. public int getyearOfPublication()
  69.    {
  70.        return yearOfPublication;
  71.    }
  72.  
  73.    public void setpublishingHouse(String publishingHouse)
  74.    {
  75.      this.publishingHouse = publishingHouse;
  76.    }
  77.  
  78. public int getpublishingHouse()
  79.    {
  80.        return publishingHouse;
  81.    }
  82. }
  83.  
  84. class Book extends Product
  85. {
  86.  private String author;
  87.  private int ISBN;
  88.  private int numberOfPages;
  89.  
  90.  
  91. public Book(String author, int ISBN, int numberOfPages, int productId, String productName, double cost,
  92.               int yearOfPublication, String publishingHouse)
  93.     {
  94.      super(productId,productName,cost,yearOfPublication,publishingHouse);      
  95.      this.author = author;
  96.      this.ISBN = ISBN;
  97.      this.numberOfPages = numberOfPages; 
  98.     }
  99. }
  100.  
  101. class Software extends Product
  102. {
  103.  private int RAM;
  104.  private int processor;
  105.  
  106.  
  107.  
  108.  public Software(int RAM,int processor,int productId, String productName, double cost,
  109.                   int yearOfPublication, String publishingHouse)
  110.      {
  111.          super(productId,productName,cost,yearOfPublication,publishingHouse);      
  112.          this.RAM = RAM;
  113.          this.processor = processor; 
  114.      }
  115.  
  116.  
  117. }
  118.  
  119. }
Jul 2 '15 #1
2 2231
chaarmann
785 Expert 512MB
I can't see any error here regarding static and non-static calls. Which line is the error? Maybe you forgot to include the line containing the error in this listing?)
Jul 6 '15 #2
You don't identify the static versus non-static methods except that the java main function is static by design. If you are calling the Product class within the java main function, then that could be a problem. Try making Product a standalone class such as Product.java. Then in the java static main method instantiate the class, e.g. Product p = new Product();
Jun 15 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: baby pink | last post by:
kindly solve the problem in article one .. its quite urgent thanx for reading goog luck pink
1
by: BVM | last post by:
Hi, All: I have this error. It seems execution time is too long. Actually the execution time is about 30 seconds(I tested in Query analyzer). How do I solve this problem? ...
7
by: Mat | last post by:
I am developping multi-user windows application. i use Access database. user edit, add and delete data from database. Request: when an item is deleted ,added or modified by an user, all others...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
2
by: begeiste | last post by:
I have installed the easyPHP on Windows, but the apache server can not be started since message shows me another web server use the web port. Don't know where to find this web port or how can I solve...
0
jautrus
by: jautrus | last post by:
I would like to have div tag over list box, not list box over div tag. I have this problem on many pages usually when I use drop down menus. I try to solve this problem with z-index but it doesn't...
16
by: Hooyoo | last post by:
Following are similar codes of my project: At first I define two classes in two files: //ClassA.h #pragma once #include "ClassB.h" class ClassA { public: ClassA(void){};
2
by: aintnoname | last post by:
I have a problem with dr java. Everything was fine until today - I got a message that no compiler is available. I downloaded the new beta version of dr java, and now there is a compiler, but when I...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
2
by: Hon Man | last post by:
how can isolve this problem in my balckberry steps to solve java.nullpointerexception
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.