aspose file tools*
The moose likes Beginning Java and the fly likes How do i avoid to use static on pretty much everything? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i avoid to use static on pretty much everything?" Watch "How do i avoid to use static on pretty much everything?" New topic
Author

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

simon onaiari
Greenhorn

Joined: Mar 19, 2013
Posts: 4
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?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And welcome to the Ranch
simon onaiari
Greenhorn

Joined: Mar 19, 2013
Posts: 4
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

yet it still asks me to create static methods / variables

Could you elaborate more with an example?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
simon onaiari
Greenhorn

Joined: Mar 19, 2013
Posts: 4
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

Jeff

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 09, 2011
Posts: 1030
    
    5

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

Joined: Mar 19, 2013
Posts: 4
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

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do i avoid to use static on pretty much everything?