The moose likes Beginning Java and the fly likes Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help" Watch "Help" New topic


James Sinarit

Joined: Mar 13, 2005
Posts: 1
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

Joined: Jul 08, 2003
Posts: 24189

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();

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!

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
subject: Help