Results 1 to 4 of 4
- 10-26-2013, 02:19 AM #1
Member
- Join Date
- Oct 2013
- Posts
- 13
- Rep Power
- 0
Should i use static method or getter?
Lets say, Class A and Class B.
Class A has the method i want to access.
Class B is where i am going to call the method from Class A.
So, in terms of performance and usability etc.. which is better?
Please explain in simple terms since i am still at the beginner stages.
- Thank you for your time :)
- 10-26-2013, 02:44 AM #2
Member
- Join Date
- Oct 2013
- Posts
- 13
- Rep Power
- 0
Re: Should i use static method or getter?
Use a static method if possible.
It doesn't make a significant difference to be honest.
Especially since you don't have to create a object to access the method for that class
Java Code:class Test { static void method() { ... } }
Java Code:class Main { static void methodTwo() { Test.method(); } }
Java Code:class Main { Test t; void methodTwo() { t.method(); } }
It's just a much more viable option without because you can access any variable statically.Last edited by Icandoit; 10-26-2013 at 02:54 AM.
- 10-26-2013, 02:55 AM #3
Member
- Join Date
- Oct 2013
- Posts
- 13
- Rep Power
- 0
Re: Should i use static method or getter?
Thank you for helping out, still looking for more opinions :)
- 10-26-2013, 04:13 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Should i use static method or getter?
In general you should avoid using static methods unless it is simply a method which is used for utility purposes (e.g. the Math class) and does not require state information from the class itself. If you start using too many static methods in your classes you defeat the purpose of OOP.
How does class B use the method? Does class B require an instance of class A?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
getter and setter method help please!
By merdzins in forum New To JavaReplies: 2Last Post: 12-06-2010, 06:06 AM -
A private static ArrayList hold an object and static getter ArrayList
By Louis in forum New To JavaReplies: 2Last Post: 11-16-2010, 06:51 PM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 06:22 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 11:03 AM -
problem in getter method
By freddieMaize in forum Advanced JavaReplies: 5Last Post: 05-26-2008, 08:02 AM
Bookmarks