• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do i avoid to use static on pretty much everything?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i created a simple program for the purpose of learning relationships in java. i seem however to get stuck with always using static when i need variables from other methods. for example:


can i call the methods getname() and car.getRegNum() without making the variables and methods static?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create an instance of the class in question, and refer to the non-static members through a reference to that instance.


 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
simon onaiari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Create an instance of the class in question, and refer to the non-static members through a reference to that instance.




i did exactly that if you look at the first few lines of code in the example i posted. yet it still asks me to create static methods / variables :<

cleaned it up a bit





Campbell Ritchie wrote:And welcome to the Ranch


thanks
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest that sort of information is better passed to the object via its constructor. I think every field ought to be initialised to a “real” value by the time the constructor completes. Look at the Java Tutorials.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yet it still asks me to create static methods / variables


Could you elaborate more with an example?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sim on wrote:i did exactly that if you look at the first few lines of code in the example i posted. yet it still asks me to create static methods / variables :<


Because you still have static stuff in your classes.

My advice: Don't put static on ANYTHING except main() for the moment. There'll be plenty of time to discover when and where to use it properly.

Winston
 
simon onaiari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

sim on wrote:i did exactly that if you look at the first few lines of code in the example i posted. yet it still asks me to create static methods / variables :<


Because you still have static stuff in your classes.

My advice: Don't put static on ANYTHING except main() for the moment. There'll be plenty of time to discover when and where to use it properly.

Winston



i removed all of the static calls execpt on the main but i still get the message that they need to be static in order for me to call Car.getRegNum() inside carOwner


James Boswell wrote:

yet it still asks me to create static methods / variables


Could you elaborate more with an example?



In the class carOwner in my orginal post if i i want to call methods from the class Car to use in the class carOwner the compiler ask me to make the methods and their variables static

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sim on wrote:

Jeff Verdegan wrote:Create an instance of the class in question, and refer to the non-static members through a reference to that instance.




i did exactly that if you look at the first few lines of code in the example i posted. yet it still asks me to create static methods / variables :<

cleaned it up a bit



In the above, there is no need for any of the following to be static: setRegNum, setModel, setBrand, setName, setAge, setAddress, Buy. If it's still giving you complaints about non-static members in a static context, it's not for the above.

As a side note, the carOwner class should be CarOwner with a capital C and the Buy method should be buy with a lowercase b, to follow Java conventions.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sim on wrote:
i removed all of the static calls execpt on the main but i still get the message that they need to be static in order for me to call Car.getRegNum() inside carOwner



Describing your code doesn't help. Show your current code, and copy/paste the exact, complete error message, and indicate clearly which line is causing it.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Buy and Sell methods (or buy and sell as Jeff rightly points out), you are calling methods on the Car class statically.
E.g.

As you are passing a Car reference c to the methods, you should be doing this:
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sim on wrote:i removed all of the static calls execpt on the main but i still get the message


Because you didn't follow the rest of my advice:
Don't put static on ANYTHING.
I'm guessing you still have the word static in your classes somewhere.

Winston
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that is the problem. The OP's code is calling methods statically when they aren't static and the compiler is complaining. However, in IDEs such as eclipse, suggestions are made to the developer on how to remedy such problems. One of these suggestions will be to make the methods static. So there are no static members in the code, it is simply that the methods are being invoked in an incorrect manner.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:I don't think that is the problem. The OP's code is calling methods statically when they aren't static and the compiler is complaining.



@sim on : Is that what's happening? That is, something like this:


If that's the case, then what you're doing is legal syntax--your code will compile and run--but it's poor form. You're calling a static method through a reference, which makes it look (to humans reading the code) like you're calling a non-static method.

If the method in question should legitimately be static, then call it with the class name, not a reference. Otherwise, make it a non-static method and call it through a reference.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff

This is what is happening (look at few posts back)


The call to the bar method assumes it is static which it isn't.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Jeff

This is what is happening (look at few posts back)


The call to the bar method assumes it is static which it isn't.



I think we're going in circles here.

Since bar() is non-static, you have to use foo.bar(), not Foo.bar().

If bar() were static, you could legally use foo.bar(), and the compiler might give you a warning, but it would still successfully compile and run, but it would be better form to use Foo.bar().

At this point I honestly have no idea what problem you're having or what it is you don't understand.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff

I think we need the OP at this point to enlighten us further.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Jeff

I think we need the OP at this point to enlighten us further.



I agree. That's why I asked, several posts back, for current code and the exact, complete error message.

@OP: And that code should not be just a snippet. It should be an SSCCE.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:I don't think that is the problem. The OP's code is calling methods statically...


And he then presented us with code that doesn't call anything statically, yet he's still got a Person class with a static name field and method. That's the problem, based on what I've seen; hence my advice.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, not to beat a dead horse here, but...

@OP: I hope it's clear from the recent comments that you need to clarify exactly what you're doing and exactly what the results are.


With an SSCCE and the exact, complete error message.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear/Jeff

Right, here is my take on this.

The last time the OP posted was at 10:05:52. Here is the code he presented in that post:

Hence, my comment that he is attempting to call non-static methods in a static manner i.e. the calls to getRegNum()

[Edit]I think the OP has left us to it
 
simon onaiari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:In the Buy and Sell methods (or buy and sell as Jeff rightly points out), you are calling methods on the Car class statically.
E.g.

As you are passing a Car reference c to the methods, you should be doing this:



haha, omg , thank you


James Boswell wrote:Bear/Jeff

Right, here is my take on this.

The last time the OP posted was at 10:05:52. Here is the code he presented in that post:

Hence, my comment that he is attempting to call non-static methods in a static manner i.e. the calls to getRegNum()

[Edit]I think the OP has left us to it



i have not left, i was simply tending to other matters.

anyways changing the class carOwner to:


as you can see its now instead of

fixed every problem, no need for any static calls any more

thanks for the help

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic