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?

5
  • I think you want n to be static. Commented Nov 24, 2013 at 19:13
  • "n" have to be public & static, or private & static bud, you have to have method to Get n :) Commented Nov 24, 2013 at 19:14
  • Which one? One from AdjacencyList class or node class? Commented Nov 24, 2013 at 19:15
  • AdjacencyList class. Commented Nov 24, 2013 at 19:15
  • How exactly do you plan to use this n from main and where do you want to create the AjacentList, in the main? Commented Nov 24, 2013 at 19:19

5 Answers 5

7

change to

public static int n; 

then you can access anywhere like this..

AdjacencyList.n 
Sign up to request clarification or add additional context in comments.

4 Comments

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 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.
I would change the name of variable. It has already started confusing me.. @iberbeu May I know why variables should not be public?
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?
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.

Comments

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

Comments

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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.