Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

When is an object a variable and when is a variable an object?

This may be elementary, but please clear this up for me: This is a challenge question: "Declare an int variable named randomNumber and, without setting a range, use the randomGenerator variable and its nextInt() method to get a random number." So,

  1. Random randomGenerator = new Random();
  2. int randomNumber = randomGenerator.nextInt();

I thought Line 1 created an OBJECT named randomGenerator of the Random CLASS. ,So, why does the question say "use the randomGenerator variable and its nextInt() method"? I thought randomGenerator was an OBJECT, not a VARIABLE?? I didn't know VARIABLES could have their own METHODS??

Thanks.

Hiya,

Which course is this in - I can't find it directly. I'd like to nswer your question whilst looking at the challenge you are taking.

Cheers,

Steve.

3 Answers

Hi Joe,

Yes, but as it is a method that takes no parameters, it needs to be .length().

I tried in in Android Studio just now to check:

    public String aVariable = "This is a String";
    public int howLong = aVariable.length();

That works just fine. Hope that helps! It is a demonstration that variables can be instances of classes and can have methods called on them. Try it yourself - I used a string as an example. Try a Double:

    public Double aNumber = 0.0;

On the next line type:

public int variable = aNumber.

At that point, the completion assistant should show you all the methods that you can call on a Double as soon as you type the dot.

Hope that helps.

Steve.

Hi Joe,

Found it - I remembered answering another query on that challenge so had a look for that response. That's here, not that it is anything to do with your question.

I can see why you asked as that question is posed using the word variable rather than instance or object. However, it is a variable; it is a word that you assign something to. It is also an object/instance of the Random class, as you say.

A variable can have methods, yes. Take a string, for example ... you can use methods of the String class to return values such as its length, for example. In this case, you've created an instance of Random which has its own methods defined within it, as well as any that it inherits from any super class.

So, you're OK with a variable being something like String aVariable = "This is a string"; - that's declaring a variable called aVariable and assigning a string to it. That's using an implicit constructor of the String class, rather than the explicit constructor used in the Random class. But it is doing the same thing, in essence.

Have a look through this post to see if that helps you out. That explains a similar thing.

Steve.

Steve, so in your example - 'String aVariable = "This is a string."; -, would something like this be ok?

println (aVariable.length);