Advertisement

Methods accessing each others variables

Started by July 16, 2017 02:47 PM
5 comments, last by Kylotan 7 years, 4 months ago

Hi guys,

So I'm wondering why cant I reference a value or even access a value that's in a method from another method.. like so

 


public class Main {

    public static void main(String arg[])
    {
                
        String bob = "bobby";
        compute();
    }

    public static void compute() 
    {
    main.bob = "yoyo";
    
    }
}


 

I don't know if I'd ever want to do this, but I'm just curious, does this mean that there's simply no other way to modify the variable values of a method without providing it with values through invoking the method ?  

 

So in the following way I can basically set what values (true to the predefined data types) these argument variables will have..


public class Main {

    public static void main(String arg[])
    {
        
        String one = "Ping",two = "Pong";
        String Result = "";
        Result = compute( one,  two);
        System.out.println(Result);
    }

    public static  String compute(String first, String second) 
    {
        
        String joined = first + " " + second;
    return (joined);
    
    }
}

 

What if instead of this I have

 

        String joined = first + " " + second;

        String joined_2 = third + " " + forth;

        String Altogether = joined + joined_2;
    return (Altogether);

 

So say i get to the first line in that code and suddenly want to change a value in the second line, would that be possible ? 

 

Okay crappy example, I suppose parameters are then the only manner available for methods to get values from the invoking method ? 

 

Most languages (including C# of Java, which you appear to maybe be using) are not designed this way; they simply don't consider a method to have local instance variables and state associated with it that persists outside the temporary memory reserved for that method when it is executing. In fact, I don't know any languages (offhand) that would explicitly permit you to do what you wrote the initial example for.

Some languages do have "static" local variables in methods, but they're usually just like globals with a narrower lexical scope; explicitly narrowed so you cannot refer to them like you're suggesting.

So yes, there's no way to modify the value of a variable in a method without passing a value to that method (or similar) to indicate how the method should initialize the variable. The (non-static) variables you're declaring in a method are local: they come into being when the method is called, and exist only for the duration of the method's execution. Then they are (effectively) destroyed when the method ends. So there's nothing to "modify" outside that method.

45 minutes ago, Xer0botXer0 said:

Okay crappy example, I suppose parameters are then the only manner available for methods to get values from the invoking method ? 

 

If you want to "share" information you need to either pass it from method to method or store it as a member of the containing class. Those are generally your two best options, and there are cases where either choice is the better one. It depends on what you're doing.

You can also create globals and share information that way, but this is generally not a great option (and not directly supported by C#, but you can do things to create effective globals that share all the same problems anyhow).

Advertisement

Thanks, well I'm using Java. And I was thinking of objects in Game Makers GML, where variables of objects can be referenced  and modified.

But I've come to realize that methods in Java are similar to Scripts in GML which are temporary. So what I'm looking for I think would be classes. I recall now that I can access variables/arrays and such from one class to another using the classname.variablename line. 

I'm mainly trying to get a better image of what everything can and cant do while studying. 

18 minutes ago, Xer0botXer0 said:

Thanks, well I'm using Java. And I was thinking of objects in Game Makers GML, where variables of objects can be referenced  and modified.

 

Variables stored in objects can be referenced and modified in Java. Methods, however, are not objects in that sense.

19 minutes ago, Xer0botXer0 said:

So what I'm looking for I think would be classes. I recall now that I can access variables/arrays and such from one class to another using the classname.variablename line. 

Yes. A class describes a type of object, and you can create instances of that type. Those instances are the kinds of objects you can read and write the member variables of.

So I did a bit of further reading and from what I can see, There are Instance fields/Instance methods and there are Class fields/Class methods, The class fields and methods are unique to each instance(object) of a class. Where Class fields and methods are somewhat global and are recognized by their static modifier in their definition. 

I can modify the instance fields/methods by storing a reference to the object in a variable.

But I've got a question regarding the Static types, I'm going to test this out now anyway for the practice because as someone said before it helps to try it out myself too. But I like the forum since you can sometimes get more information which is apart of what helped me progress in Java quite a bit. 

If I create a class called Fruit, and within the class I create a class variable called fruit_Type, then create four objects of class Fruit, I don't use a for loop rather just one after another, in each case I set the fruit_Type class variable to something different like "mango", "orange", "tomato(is tomato a fruit lol)", "guava". 

If I then iterate through all four objects, will they have unique values for fruit_Type, can I even access a static field(var) that way ? Going to test this out now. 

 

From my understanding the purpose of a class field is to store a value that all objects of that class will share, the purposes of instance fields is that the instance values can be unique, instead of having a basket.java of mangoes I'd have a basket of a variety of fruit.

 

Okay I tested it.


public class Main {
    
    public static void main(String arg[])
    {
        
        Fruit f1 = new Fruit();
        f1.fruit_Types = "Banana";
        
        System.out.println(f1.fruit_Types);
        System.out.println("");
        
        Fruit f2 = new Fruit();
        f2.fruit_Types = "Guava";
        
        System.out.println(f1.fruit_Types);
        System.out.println(f2.fruit_Types);
        System.out.println("");
        Fruit f3 = new Fruit();
        f3.fruit_Types = "Grape";
        
        System.out.println(f1.fruit_Types);
        System.out.println(f2.fruit_Types);
        System.out.println(f3.fruit_Types);
        System.out.println("");
    }
}

 

Fruit.java :: public static String fruit_Types = "Lemon"; produces:

  • Banana
  •  
  • Guava
  • Guava
  •  
  • Grape
  • Grape
  • Grape

while without the static modifier it becomes an instance field and produces:

  • Banana
  •  
  • Banana
  • Guava
  •  
  • Banana
  • Guava
  • Grape

 

So I'm happy with the findings. :D

Static, in this context, does mean "shared across all instances of this class". Everything else has its own copy per instance. Good work on testing for yourself and verifying your understanding. Try and avoid statics unless you absolutely need something shared, however.

This topic is closed to new replies.

Advertisement