473,387 Members | 1,619 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,387 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 2235
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.