1

I have two classes:

public class node {
  static LinkedList<Integer> nodes = new LinkedList<Integer>();
  public boolean visited;

  public static void Main (String args []) {
    System.out.println("Number of nodes in network");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    for (int i=1;i<=n;i++){
      nodes.add(i);
    }

    System.out.println(nodes);
  }

and another class

public class AdjacencyList {
  private int n;
  private int density;

I want to access int n from Mainmethod and assign its value to private int n in AdjacencyList class. I tried node.n (class.variable) format in a method where I can assign values but its not working out. Can anybody please help me with this?

Gaurav K.
  • 67
  • 2
  • 3
  • 10

5 Answers5

7

change to

public static int n;

then you can access anywhere like this..

AdjacencyList.n
subash
  • 3,116
  • 3
  • 18
  • 22
  • 1
    This is actually a pretty bad solution. Variables shouldn't be public. Any way, if you want to do that ´AdjacencyList´ must be static – iberbeu Nov 24 '13 at 19:15
  • @iberbeu When its a small program why not.Not like its going to confuse anyone.Bud he shoud pick another name for that variable this might be confusing later on. – Tomas Bisciak Nov 24 '13 at 19:19
  • I would change the name of variable. It has already started confusing me.. @iberbeu May I know why variables should not be public? – Gaurav K. Nov 24 '13 at 21:27
  • As a common pattern variables should be declared as private. You then will provide getter and setter for getting acces to it. If you do it so, you ensure that your class is allways managing the way its fields are changing. So normally, declaring attributes as public is a bad thing. Sometimes it is needed though. Any way I don't recomend it. The question could be: why should a variable be private? – iberbeu Nov 24 '13 at 22:33
3

Simply,

public class AdjacencyList {
    private int n;
    private int density;

    //method to get value of n
    public int getN() { return this.n; }

    //method to set n to new value
    public void setN(final int n) { this.n = n; }

Then, in your main(), you can do this:

AdjacencyList myList = new AdjacencyList();

//set n to any value, here 10
myList.setN(10);

//get n's current value
int currentN = myList.getN();

All this is basic java stuff, please go through the docs again, especially here and here.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
0

You can't. Local variables (variables defined inside functions) have scope limited to this function (here Main, note: it is practice to start with lowercase character for function names).

If you want some variable to be accessed from outside, it needs to be class variable and that you declare functions to access it ( setN / getN or similar...)

Also, as function is static, variable also needs to be static.

Hope it helps

igr
  • 3,409
  • 1
  • 20
  • 25
0

You can do that or you can create a new instance of AdjacencyList and set n in the constructor. Then access n with a get() function.

Dave Hulse
  • 70
  • 6
0

Add to the second class public setter for n, then create an instance of AjacencyList in main and call the setter.

quez
  • 1
  • 2