Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,147 Members 48,842 Now Online
XDA Developers Android and Mobile Development Forum

[Q] Java/android not understanding oop

Tip us?
(Last edited by madmcphil; 3rd May 2012 at 02:27 PM.)
Junior Member - OP
Thanks Meter 0
Posts: 22
Join Date: Jun 2010
Default [Q] Java/android not understanding oop

Hi guys,

Im really trying to learn java but think Im getting very confused. I have a class which holds names and information, In my android program I have the user enter a load of names which then are passed as an array to my next activity. Whenever I try to create objects from each name in the array it wont do it? Ialso understand that this may be a bad way of doing this as names will change so how would I reference them. Maybe using the array[pos] to create objects... so instead of name.printweight(); I could use 1.printweight();

I really hope above is readable or understandable as to what I am trying to learn


Take in names of competitors
Pass array of competitors to next activity
for loop to get each competitor name entered to a string
create an object using the competitors name (e.g name.birthday)

class competitor:
highest score

I think you cant use a string name or an int to create objects but how else would I create several competitors in my class?

Im reading some books I bought and I really am struggling to make sense of all this. Please be patient with me...

Kind regards

Junior Member - OP
Thanks Meter 0
Posts: 22
Join Date: Jun 2010
Think I got it.....

for (int i = 0; i < numcompetitors; i++)
String competitorName = (String) competitorsArray[i];
TextView txtOutput = (TextView) findViewById(;
txtOutput.append(competitorName + " ");
//create an object of this competitor
competitorsArray[i] = new Competitor();
((Competitor) competitorsArray[i]).average();
Senior Member
Thanks Meter 45
Posts: 153
Join Date: Jan 2011

I couldn't really understand what you are having a problem with. The code you posted might be working but it really doesn't seem like a good way of doing what you are trying to do.

I'd like to help you out but first can you please explain a bit more clearly? Maybe even post your code, so that its easier for me to understand.
RoberGalarga's Avatar
Senior Member
Thanks Meter 260
Posts: 730
Join Date: Dec 2011
Location: In front of my laptop :D
So... if I understood you, you need something like this:
class competitor:
highest score

Names entered:

A.dob, A.weight, A.highestscore, A.time
B.dob, B.weight, B.highestscore, B.time
C.dob, C.weight, C.highestscore, C.time

Right? Well, it can't be achieved using Java, it doesn't allow you to assign programatically names to variables. However, you can use a little trick based on an array:
//Remember: you have a class named "competitor" containing several variables
String[] Names= {"Uno","Dos","Tres"};  //the array passed by the other activity
competitor[] playerData=new competitor[Names.length];  //an array, to store copies of the competitor class (a copy for each name entered)

for(byte i=0;i<Names.length;i++){
  playerData[i]=new competitor();  //Initializing the copy...
    playerData[i].dob=     //Storing data for each name entered...
And that's all... 1st player's data will be stored in the position 0 of the array, 2nd player's data in the position 1... and so on.

BTW, instead use 2 activities, is more easy to use only an activity, changing its layout when you need a GUI's change (yep, it's possible ).
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes