Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Should i use static method or getter?

  1. #1
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default 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 :)

  2. #2
    Icandoit is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default 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();
          } 
    }
    VS

    Java Code:
    class Main {
          Test t;
          void methodTwo() {
                t.method();
          }
    }
    Just E.G.

    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.

  3. #3
    MGT Madness is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: Should i use static method or getter?

    Thank you for helping out, still looking for more opinions :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default 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,
    Jim
    Tolls likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. getter and setter method help please!
    By merdzins in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 06:06 AM
  2. Replies: 2
    Last Post: 11-16-2010, 06:51 PM
  3. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. problem in getter method
    By freddieMaize in forum Advanced Java
    Replies: 5
    Last Post: 05-26-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •