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 Main
method 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?
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.