Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to set a variable equal to a class?

I want set an array value equal to a variable int in another class. This seems dumb but this is just one small part of what i want to do, but i can't figure this out.

I want to take,

anArray[1] in class 'A'

and set it equal to variable 'int x' in class 'B'

I don't want to do this by going

anArray[1] = B.x;

because the class i want to grab the variable from will change. So i want to create a variable "className" and set it equal to class.B

then i could go

anArray[1] = className.x;

and when i want to grab x from a different class, i just change className from equal to class.B to equals class.C for example, which would also hold a public static int x.

How can something like this be done?

2 Answers

Relevance
  • 8 years ago
    Favorite Answer

    You have too man sentence fragments and things that are wrote wrong I can hardly understand your questions.

    You have three classes in a directory.

    You refer to them by creating a new object...

    ClassName varname = new ClassName();

    Your variables set inside the ClassName are set by the ()'s

    Your class should be set up like this:

    public class Main{

    public static void main(String[] args){

    ClassName cn = new ClassName("apples");

    }

    }

    then your other class

    public class ClassName{

    private String Name;

    ClassName(String name){

    this.Name = name;

    }

    public String getName(){

    return Name;

    }

    }

    Therefore speaking with the second class you can say

    String name = cn.getName();

    This would return the current name when executed. It will always return the set value stored in cn. Once you change the value inside the class with... idk... changeName(); it would then return the current value of it.

    to declare this variable a set value without it going changing on you... and no matter how many times you change the name inside that class... you call final with it.

    SOOOO:

    final String name = cn.getName();

    Can't be changed nor returned a different value then it when executed.

    In all cases if this was not what your looking for feel free to send me a message reguarding this.

    I will happily overlook your scripts to.

    - http://answers.yahoo.com/ Freelance developer question answerer

    - Souly helping to follow Jesus and help novice coders!

    - I teach Java, HTML, CSS, JavaScript (Email me I teach all)

  • ?
    Lv 4
    5 years ago

    Objects move via reference in Java. So... Int [][] array2Dim at x0AB191 memory in RAM int [][] array2nd is the same as x0AB191 reminiscence in RAM hence, exchange a sub-script on one Object, you see the reflection on all copies. Array.Clone() is a shallow copy additionally. You can write the loops to make a distinctive Object or serialize. Are you engaged on 9 queens?

Still have questions? Get your answers by asking now.