-2

This is sample reference example, for what I am looking Solution

Reference Example:

  • Here demoName is global variable
  • Somehow I need to define that variable on Class level which is : String demo = demoName;
  • Now it needs to override variable value with local variable, when it call from local method.

    void test(String name) { demoName = name; System.out.println("Local Value:" + demoName); System.out.println("Global Value:" + demo); }

Here demoName becoming override with parameter value, But when +demo is taking class level value which is XYZ, I want it to be abc.

class demo1 {
    public static String demoName = "xyz";
}

public class demos extends demo1 {

    String demo = demoName;

    void test(String name) {
        demoName = name;
        System.out.println("Local Value:" + demoName);
        System.out.println("Global Value:" + demo);
    }

    @Test
    public void testtest() {
        test("abc");
    }    
}

I want both value to be "abc" .

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
  • That is, because `By demo = By.xpath(demoName);` is initialized when the class is created. And at this moment `demoName` is equal to `"xyz"` – Lino Jul 25 '18 at 12:27
  • Yes, Somehow I need to declare it on Class level. But, I am looking for is there is any possibility where it can override by local assignment. Something from this or Super. – Ishita Shah Jul 25 '18 at 12:30
  • Why not just declare `demo` inside the `test` method? – Lino Jul 25 '18 at 12:33
  • 1
    When you're going to use outdated programming patterns (like global variables and static access) why don't you choose a Language that supports that more like C/C++? – Timothy Truckle Jul 25 '18 at 12:35
  • 1
    A variable is only evaluated once on construction. If you want it to be re-evaluated you need a method. – Peter Lawrey Jul 25 '18 at 12:35
  • @Lino For some internal architecture, I need to declare it on class level. – Ishita Shah Jul 25 '18 at 12:36
  • Why can't you just update `demo` the same way you update`demoName`? – Max Vollmer Jul 25 '18 at 12:42
  • 1
    @IshitaShah Whatever drives this requirement, please, do not call that *architecture*. What you are doing here is the exact opposite of a reasonable architecture. – GhostCat Jul 25 '18 at 12:43
  • 2
    Then: there is no need to put up screen shots here. Test code, and JUNit output, that is all text, and could be included as text. – GhostCat Jul 25 '18 at 12:43
  • Possible duplicate of [Is there a way to override class variables in Java?](https://stackoverflow.com/questions/685300/is-there-a-way-to-override-class-variables-in-java) – Frederik Vantroys Jul 25 '18 at 12:45
  • @FrederikVantroys I have referred that solution, But its different scenario. Its override on class variable only, so static block is applicable. I have my local dynamic value by parameterized. I am getting dynamic value on method call. – Ishita Shah Jul 25 '18 at 12:50
  • Could this be an XY problem? You're looking for a "reference" variable, it seems. Could you explain what problem you're trying to solve? –  Jul 25 '18 at 13:18
  • @Arkadiy I am using things for Automation. I need to by pass one of the variable in to script query which is defined on class level. I do get variable value dynamic on method call. So as of now, I am assignning value of parameterized variable to global variable, which is part of script query. I want it to be override by local value, somehow if possible. – Ishita Shah Jul 26 '18 at 04:11

2 Answers2

2

Because demoName is public, you can reassign it from anywhere using Demo.demoName = "abc";, no need for a subClass at all.

However, using a public static variable and reassignating is awful. If you just want to override it at instance level in a subclass, you should use an accessor and override the accessor :

public static class Demo {
    public static String demoName = "xyz";

    public String getDemoName() {
        return demoName;
    }
}

public static class Demos extends Demo {

    private String demoNameOverride;

    @Override
    public String getDemoName() {
        return demoNameOverride;
    }

}
Jeremy Grand
  • 2,300
  • 12
  • 18
0

String is Immutable in java

Note: when two String variables are referencing to the same object, if one variable changes its object at any stage , it only changes that variable Object not the other variable Objects.

 String demo = demoName;

at this line of code both variables are referencing to the same object xyz which means demo and demoName have xyz. the reason why it is not changing its object after

@Test
    public void testtest() {
        test("abc");
    }  

because String is immutable in Java. please refer Why is String immutable in Java? https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/

ex:

static String a="Helloworld";
static String b=a;

public static void main(String[] args) {

    System.out.println(a);
    System.out.println(b);
    a="World";
    System.out.println(a);
    System.out.println(b);

}

output:

 Helloworld
 Helloworld
 World 
 Helloworld

this is how your programme logic flows now and b is referencing to its previous Object Only.after you change the String Value and again the reference has to be done.The below example will give you the idea.

public class Demo1 {

    public static String demoName = "xyz";
}

class Demo extends Demo1 {

    static String demo = demoName;

    public void test(String  variableName) {
        //initially i am calling both varaibles which you assign
        System.out.println(demoName);
        System.out.println(demo);
        // i am assigning the "abc" to demoName and calling both varaibales
        demoName = variableName;
        System.out.println(demoName);
        System.out.println(demo);
        // Now  i am assigning the "abc" to demo and calling both varaibales
        this.demo = variableName;
        System.out.println(demoName);
        System.out.println(demo);

    }

    public static void main(String[] args) {
        Demo dem = new Demo();
        dem.test("abc");
    }

}

The Output is :

xyz
xyz
abc
xyz
abc
abc
RamanaMuttana
  • 481
  • 6
  • 22