wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Can Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can "subscripts" be used when naming object references?" Watch "Can "subscripts" be used when naming object references?" New topic
Author

Can "subscripts" be used when naming object references?

Michael Yiasemides
Greenhorn

Joined: Feb 28, 2012
Posts: 7
Hi I would like to know if something like subscripts can be used when naming object references. Subscripts might not be the best word to use, but it's hard to explain what I mean so I will give an example:

I would like to be able to do something like this:

for(0<=x<=5){
JButton buttonx = new JButton;
}

which will create 6 buttons for me: button0, button1, ... , button5.

Is something like this possible?


Also one last small, perhaps trivial question. Say buttonArrayList is an ArrayList of JButton, is

for(JButton button : buttonArrayList){
button.setText("Hi")
//Other code that does stuff with button
}

the correct way to say: for all Jbuttons in buttonArrayList do whatever it says in the curly braces?

Thanks

Mikey117


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Michael Yiasemides wrote:
for(0<=x<=5){

even below code will not
Michael Yiasemides
Greenhorn

Joined: Feb 28, 2012
Posts: 7
Seetharaman Venkatasamy wrote:
Michael Yiasemides wrote:
for(0<=x<=5){

even below code will not


Unfortunately I don't understand what you have said. Could you clarify please? Thanks.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3499
    
  13
Michael Yiasemides wrote:Hi I would like to know if something like subscripts can be used when naming object references


No, but you could store your variables in an array or Collection


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

This is not valid java syntax:


It won't compile.

The correct format would be (assuming x is declared somewhere else:


But even that isn't the normal idiom...you'd normally write it like this:


The problem with what you are trying to do is that your buttonx would only exist inside your for loop, due to scoping rules. You would have to declare the variables outside of the for loop.

Or, as Joanne says, use an array or other collection.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Yiasemides
Greenhorn

Joined: Feb 28, 2012
Posts: 7
Thank you. I will try to use arrays to do what I had in mind.

Also thanks for informing me that my buttons will only exist in my loop. I also had a similar problem where my JFrame etc only exists inside my GUI building method, and I've just realised... It's my first time programming.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Don't you think it's a little early to begin with Swing even if you're obviously not familiar with basic Java constructs such as for loop? I would recommend you go one step at a time.


The quieter you are, the more you are able to hear.
Michael Yiasemides
Greenhorn

Joined: Feb 28, 2012
Posts: 7
Kemal Sokolovic wrote:Don't you think it's a little early to begin with Swing even if you're obviously not familiar with basic Java constructs such as for loop? I would recommend you go one step at a time.


I wouldn't say I am unfamiliar with a for loop. Perhaps with the syntax yes. The fact I forgot that the Buttons would only exist inside the for loop comes down to inexperience with Java. I have read Head First Java and so I am familiar with java to an extent but really need to actually write some code to improve.

When I said it was my first time programming that wasn't entirely true. I have used (not much though) MuPad on MatLab as part of my university course. The for loops there work slightly differently you see.

Besides I am only trying to make a simple noughts and crosses programme. I've made one that only works at the command line and other programmes that find primes and Fibonacci numbers etc. I don't think this is such a big step.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

My intention was to give you an advice not to rush to Swing before you are sure you are well familiar with basic of Java (that includes syntax mentioned above). If you really want to learn to develop software with Java or any other programming language, that is the only correct path you should follow. If you are familiar with programming logic (from Matlab or any other) that's just a plus and will make the learning path less steep.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2343
    
  28

For a very fresh programmer, I would consider a problem like the one you had regarding the buttons being scoped inside the loop to be much more serious than remembering the syntax of a for loop. Scope is a very important concept in Java, and experienced programmers understand scope without really thinking about it. It's almost like second nature to experienced programmers. It's one thing to understand scope, it's another thing to grok it.

Beginner Programmers aren't expected to grok it like experienced programmers, but you are expected to have a very good understanding of it. I would heed Kemal's advice and not advance to Swing unless you have a good understanding of scope. Otherwise, you might hurt yourself
 
jQuery in Action, 2nd edition
 
subject: Can "subscripts" be used when naming object references?