• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

non-static method cannot be referenced from a static context

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting this error

I know that I can't get to the non static method in that class so how do I use it?mainProgVideoStore.java:80: non-static method newCVideo(java.lang.String,int) cannot be referenced from a static context
Customer.newCVideo(title, vAmount);



And this is the method I want to use. in my Customer class
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a non static method- newCVideo in Customer class- So to access this you need to create an instance of Customer Class. Or you have to declare the method as static in Customer class.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say I have to create an instance of Customer Class do you mean something like this
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but you've already done that:
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you have an instance method (or a non static method) - How do you access that method? Via the class name or via the instance name?
 
Rob Spoor
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance name.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So now I have to make the newCVideo visible I have to make it static, ok well when I try that the compiler doesn't like that either and gives me the same error message because I'm using other nonstatic methods in that method. Would I have to make all of those methods static too?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have created an Instance of Customer in line 12. You can used that to invoke the method. Rarely do we use static methods.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I use it because I thought I already was when I did the Customer.newCVList.....
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let me take a simpler example (your code is huge to explain).


So you can see in the above code- how instance method and static method are accessed. So in your context- you should be using the instance newCVideo created in line 12.

PS: This is not a fully working code- Made it simpler to understand for the OP.

Update: I have been talking about the line 80 in mainProgVideoStore. Because that's where the exception is seen.

 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so this works only because you put my in front of class Customer?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dustin Schreader wrote:so this works only because you put my in front of class Customer?



myCustomer is the name of the Customer instance- It can be anything- ourCustomer, customer, theCustomer, oneCustomer (any name which doesnt violate the naming convention, is not a keyword,reserved word and not the name of the Class).

In your case- You have a non-static method- newCVideo in Customer class. So to call/invoke this method(line 80) you would have to use an instance of Customer class (which you have created in line 12 of mainProgVideoStore). So the line 80 would now be:


Note: Pretty confusing to have the method and object name to be similar.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok I understand, to lessen the confusion I changed it to myCustomer instead, thank you!
 
reply
    Bookmark Topic Watch Topic
  • New Topic