Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help

 
James Sinarit
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help:
here is the code:

public class Integer {

public int i = 1;

public static void main(String args[]) {

}
}

class other {
public static void main(String args[]) {
System.out.println( i );
}
}

How can I make it display the variable i in the other class?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Welcome to JavaRanch!

"i" is a member variable in your Integer class. One copy will exist inside each Integer object you create. Your main method has to create an Integer, and then access its "i" member:

Integer anInteger = new Integer();
System.out.println(anInteger.i);

Note that "Integer" is the name of one of the core Java classes, and as such should be avoided as a name for your own classes!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic