File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can "subscripts" be used when naming object references?

 
Michael Yiasemides
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Yiasemides wrote:
for(0<=x<=5){

even below code will not
 
Michael Yiasemides
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Yiasemides
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Yiasemides
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic