Static vs non static methods - best practice

M

m97rek

I have a question with regards to program design.

If I have a class that looks something like:

public class SampleApp extends Engine implements Listener_OnMyEvent
{

public static void main(String argv[])
{
int x=10;

//Call metod1
Method1(x);
}

public method1(int i)
{
//Call a method from the Engine Class
//I know I don't have to have "Engine." here but it's just to
clearify
Engine.method2();
}
public void OnMyEvent()
{
Log("Event Hapended");
}
}

Let's say now that I want to move method1 to another class/file in
order to keep the code as neat and tidy as possible.

How should I do that? What is best practice according to you? Should
the methods there be static?

I tried to create:

public class MyMethods
{
public static method1(int i)
{
Engine.method2();
}

}
....but then method2 isn't available since Engine isn't implemented
unless I pass the whole class along:
MyMethods(x, this)

Is this correct?

I hope you see what I'm getting at here.
Any suggestions is appreciated.

Thanks
 
D

Daniel Pitts

m97rek said:
I have a question with regards to program design.

If I have a class that looks something like:

public class SampleApp extends Engine implements Listener_OnMyEvent
{

public static void main(String argv[])
{
int x=10;

//Call metod1
Method1(x);
}

public method1(int i)
{
//Call a method from the Engine Class
//I know I don't have to have "Engine." here but it's just to
clearify
Engine.method2();
}
public void OnMyEvent()
{
Log("Event Hapended");
}
}

Let's say now that I want to move method1 to another class/file in
order to keep the code as neat and tidy as possible.

How should I do that? What is best practice according to you? Should
the methods there be static?

I tried to create:

public class MyMethods
{
public static method1(int i)
{
Engine.method2();
}

}
....but then method2 isn't available since Engine isn't implemented
unless I pass the whole class along:
MyMethods(x, this)

Is this correct?

I hope you see what I'm getting at here.
Any suggestions is appreciated.

Thanks
Actually, static means it IS accessible by Engine.method2();

Though in general an abundance of static methods suggest that your OO
design has a flaw.
 
L

Lew

m97rek said:
If I have a class that looks something like:

public class SampleApp extends Engine implements Listener_OnMyEvent
{

public static void main(String argv[])
{
int x=10;

//Call metod1
Method1(x);

You don't show us any method "Method1(int)". What is that method?

That's not an engineering reason. Methods go with the class whose behavior
they implement.

Static methods are for class-wide behaviors. It's not about making things
"neat and tidy". If a behavior belongs to the whole class, as with utility
classes or factory methods, then make the method static, otherwise make it
instance-level. If you're not sure, and the class is instantiable, make it an
instance method.

Methods reflect your object model. If your model says that there is a
business object type "Foo", and that Foo thingies do certain behaviors, then
you implement that with a class Foo and methods in that class implement those
behaviors (preferring instance level over static where feasible).

Huh? You lost me on this point. What is Engine?

Since its name begins with an upper-case letter, it must be a class. Since
you invoke method2() (no arguments) via Engine, it must be a static method.

MyMethods(x, this) is a constructor, but you haven't shown us this constructor.

No.

You really, really need to read and follow
<http://www.physci.org/codes/sscce.html>

Provide us with an SSCCE. If you can't compile the code then we can't either.
 
A

Are Nybakk

m97rek said:
I have a question with regards to program design.

If I have a class that looks something like:

public class SampleApp extends Engine implements Listener_OnMyEvent
{

public static void main(String argv[])
{
int x=10;

//Call metod1
Method1(x);
}

You shouldn't call methods from main like this. Since main is static,
all methods it calls must also be static. Let SampleApp have a
constructor that you call. Do method calls with the object:

public static void main(String args[]) {
int x = 10;

SampleApp newSampleApp = new SampleApp();
newSampleApp.method1(x);
}

public SampleApp() { ... }
*snip*
I hope you see what I'm getting at here.
Any suggestions is appreciated.

I have no idea what you want. Please clarify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top