smex
smex

Reputation: 23

How to use values from another class

I would like to have a class or whatever else is better where I store constant values that I want to use in other classes.

For example, I would like to have a class named A which looks like

public class A
{
  public int x = 1;
}

and in another class I would like to use that value in a method:

class B
{
  public void test()
  {
    int y = A.x;
  }
}

I know this is wrong, it's just an example of what I would like to use. It's probably a basic question but I don't have much experience with programming.

Upvotes: 2

Views: 4229

Answers (3)

Safwan Shaikh
Safwan Shaikh

Reputation: 546

It is the bad practice to declare everything public. Access modifiers set the accessibility of method, class or variables. Meanwhile There are four pillars of Object Oriented Programming. Abstraction, Inheritance, Polymorphism and Encapsulation. You should learn them for better and good understanding of programming, link is provided here. OOP

Hope this will surely help you.

Meanwhile in your question, if you really need to access behavior or method of class A, you can make it static and access in another class.

public class A{

    public static int x = 1; 
    public static void print(){
        //some code here
    }
}

public class B{

    public void test(){
        int a = A.x;
        A.print();
    }
}

Or another method is to make x private and make getter and setter of it. Initialize instance of A in class B and access it through its instance.

A obj = new A();
int a = obj.getX();

Upvotes: 2

auhmaan
auhmaan

Reputation: 726

What you probably want is a const or static variable, where you can define a value and access it without initializing the class.

With your example, it would be something like this:

public class A {
    public const Int32 ConstantVariable = 0;

    // or

    public static Int32 StaticVariable = 0;
}

public class B {
    public void test() {
        Int32 y = A.ConstantVariable;

        // or

        Int32 y = A.StaticVariable;
    }
}

Be aware that const variables need to be initialized with a value on the spot and cannot change value, whilst static variables can be assigned in run-time and changed whenever you want.

Upvotes: 1

Taco
Taco

Reputation: 2923

You have to create an instance of class A within class B in order to access it's properties (or in your case it's field x).

internal class A {
    public int X { get; set; } = 1;
}
internal class B {
    private A myInstanceOfA = new A();
    public void test() {
        int y = myInstanceOfA.X;
    }
}

You can however, access static and constant members of class A without creating a local instance:

internal class A {
    public const int X = 1;
}
internal class B {
    public void test() {
        int y = A.X;
    }
}

I would heavily recommend you look at the following articles:

Those should give you a thorough introduction to classes and the members contained within them.

Upvotes: 1

Related Questions