Pawan
Pawan

Reputation: 32331

Are static setter/getters allowed?

I am develoing a Web Application, in that there is a utility method named getData() which I made it as a static. Until now its fine, but this static method named getData() needs some data from setters and getters. So now my question is can we make setter/getters as static?

Upvotes: 19

Views: 89310

Answers (10)

Whitney Kugel
Whitney Kugel

Reputation: 1

Using static getters/ setters are allowed (and required) for static fields. The idea is to match, either both are static or non-static because the relationship to the class/ object is in mind. Attempting to use a non-static getter or setter on a static field causes a conflict because the value is shared and you will see a message like, “Non-static method 'getThingA()' cannot be referenced from a static context.”

This is beneficial if you are working with something that requires data tied to the class rather than specific instances of that object. It also promotes encapsulation on a class level which is one of the core pillars of OOP and simply good practice. Additionally, by using static getters and setters you allow yourself the ability to expand these calls later if needed rather than modifying code in countless other places.

Upvotes: 0

Sumit Singh
Sumit Singh

Reputation: 15896

If your properties are static, then getters and setters will also be static. It all depends on what you want to do.

Upvotes: 32

stackUser
stackUser

Reputation: 579

If your data members are static then you can use static getter/setter. But if you data members are not static then call your not static getter/setter on object in getData() static method.

Upvotes: 0

Testilla
Testilla

Reputation: 692

Yes, static setters/getters are allowed as long as both the class variable and the method are made static. Take a look at this getter method example.

public class Test {
  private static WebDriver driver;


public static WebDriver getDriver() {
    return driver;

Upvotes: 1

Pratik
Pratik

Reputation: 30855

yes you can and that class whatever that object/variable was defined they looks like

private static String abc = "";

and you can access this object using get/set method

public static String getString(){
    return abc;
}

public static void setString(String newAbc){
   abc = newAbc;
}

and you can use this like this way Test.getString(); or Test.setString("new string");

you can also define this get/set method as normal means without defined the static keyword but for that you need to create the instance of that class. The static was used for without creating an instance of the class you can access their member.

Upvotes: 15

jeha
jeha

Reputation: 10730

Of course you can make getters and setters be static (with appropriate static fields).

But: As you are dealing with a web application (multiple parallel requests - multiple threads) you surely have a threading issue here - this is not thread-safe unless you take care of (e.g. use synchronized).

Upvotes: 6

vgardner
vgardner

Reputation: 517

You can't make getter and setter methods static if you use any attributes or properties that aren't static. If you use IDEs like Eclipse and Netbeans, they'll warn you about that or might not even let you compile the code.

Upvotes: 1

biaobiaoqi
biaobiaoqi

Reputation: 1098

Sure you can . Getter and setter are just normal methods . They can be static or not .

The only constraint is , do not use non-static filed and method in the static method. As static method and static filed belong to a class ,and non-static method and field belong to the object . they are two different levels I think.

Upvotes: 1

Aditya Naidu
Aditya Naidu

Reputation: 1380

Yes, getters/setters can be made static based on your need. Or maybe I didn't understand your question!

Upvotes: 0

jornb87
jornb87

Reputation: 1461

Getters and setters can be static if they get/set static fields.

Upvotes: 13

Related Questions