Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
I am learning ArrayList. The following program is very well Working. But my question is that in Line20 below
why can't I access the variable i ?
like below
System.out.println(myObjectArray.i);
Since i is having Default access and which we suppose to be able to access in the Objects.

Thanks for the reply.

public class ObjectArray {
int i;
/** Creates a new instance of ObjectArray */
public ObjectArray(int i) {
this.i=i;
}

ObjectArray returnMyObject(){
return this;
}
public String toString(){
Integer myInteger = new Integer(this.i);
return myInteger.toString(i);

}
public static void main(String[] temp){
ArrayList objArrayList = new ArrayList();
for(int i=0;i<10;i++){
ObjectArray objObjectArray = new ObjectArray(1);
objArrayList.add(objObjectArray);
}
System.out.println(objArrayList.size());
Object myObjectArray;

for(int j=0;j<objArrayList.size();j++){
myObjectArray = objArrayList.get(j);
System.out.println(myObjectArray);
//System.out.println(myObjectArray); Here is
my Doubt
//Line 20:
}

}
}
 
The point is line 20 is within the main() method, which is the the same scope as ObjectArray class.
 
My Question is that I am not able to access like below at Line20.
System.out.println(myObjectArray.i);
In the above statement i is having Default access. So according to my inderstanding I should be able to access from main method.


 
Because myObjectArray is declared to be of type Object.
That's not an error, since everything (beside int, float, ...) is derived from Object.
And ArrayList.get (int index) returns an Object.
Fine here too.

But myObjectArray.i isn't possible, since Object doesn't have a member called i.

What you need, is a ObjectArray (dubios naming - by the way - but I guess it came from trying here and there).

Code:
ObjectArray oa;
/* Since ArrayList returns Objects, we need to cast the object  to it's derived type: */
// ...
    oa = (ObjectArray) objArrayList.get(j);
    System.out.println (oa.i);
btw:
a) it's line 27 at least!
b) If you use this constructor call, your program will make real fun :)
Code:
   ObjectArray objObjectArray = new ObjectArray(i);

seeking a job as java-programmer in Berlin:
 
As I said, you cannot access "i" of ObjectArray in main(), as main() is a special method that is called by the JVM when executing an application. main()'s scope is not considered as default access. Although main is defined inside the file of ObjectArray class, Java treats it as with any other main() that could have defined in other class outside the package. In short, you can access variable with default access modifier in any other method in any other class within the same package, but not in main().
 
Sorry, I was wrong. I was confused too. You should be able to access "i" in main().

The reason why it cannot is your class isn't define with a package. As default access applies to the same package. i.e. the following would works:

Code:
[COLOR=red]package yourPackage;[/color]

public class ObjectArray {
  int i;
  ......
  public static void main(String[] argv) {
     ObjectArray o = new ObjectArray();
     System.out.println(o.i);
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top