Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Static vs non static methods - best practice

Thread view: 
m97rek - 02 Nov 2007 21:03 GMT
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
Daniel Pitts - 02 Nov 2007 21:23 GMT
> I have a question with regards to program design.
>
[quoted text clipped - 50 lines]
>
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Lew - 02 Nov 2007 23:21 GMT
m97rek wrote:
>> If I have a class that looks something like:
>>
[quoted text clipped - 7 lines]
>>     //Call metod1
>>     Method1(x);

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

>>   }
>>
[quoted text clipped - 13 lines]
>> 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.

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

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

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).

>> I tried to create:
>>
[quoted text clipped - 9 lines]
>> unless I pass the whole class along:
>> MyMethods(x, this)

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.

>> Is this correct?

No.

>> I hope you see what I'm getting at here.

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.

> 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.

Signature

Lew

Are Nybakk - 03 Nov 2007 10:51 GMT
> I have a question with regards to program design.
>
[quoted text clipped - 10 lines]
>     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.

> Thanks


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.