File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes this is working but i don't understand why!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this is working but i don Watch "this is working but i don New topic
Author

this is working but i don't understand why!!

David J Evans
Greenhorn

Joined: Apr 28, 2006
Posts: 11
Hi, I've been working on something during my spare time at work (i'm not a java programmer at work!!), and this snippet of code works perfectly well, although I didn't expect it to, and would be grateful if someone could explain why it works, and why I am clearly wrong about how I think it should work!

Here's the code:



The bit I don't get is this: jTabbedPane1.add(choose.getDescription(files[i]),new JScrollPane(area = new JTextArea())); , and it's because I define a new JTextArea called area, and then do it several times, so in my mind, this should mean when I open several files, they should display the contents of the last file to go through the for loop iteration...but I was wrong, and I don't understand why!!

Please help me understand!

Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

That's the comma operator. All it does is evaluate the two expressions on both sides of the comma, and the result of the whole expression is the value of the expression on the right side of the comma.

It's normally not used in the way you use it. The only place where people sometimes use it is in the initialisation expression of a for-loop, like this:

The page that I link to above explains it in detail.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Each time you call "area = new JTextArea()" you create a new JTextArea on the heap and assign a reference to it to the local variable area. Then you add that JTextArea to the TabbedPane. The tabbed pane gets a new reference to this same JTextArea on the heap. While area is still in scope - hence referencing the newly created JTextArea you set its text.

Each iteration through the loop, area references a new JTextArea. so calling area.setText in one iteration has no effect on any of the other JTextAreas.
David J Evans
Greenhorn

Joined: Apr 28, 2006
Posts: 11
Tim, thanks for your reply. Sorry Jesper, but I really don't see how what you said explained what I was seeing.

Tim (or anyone else) - is what I've done safe? I mean, can I reference each instance of area, and if so, what name would I use?
[ October 19, 2006: Message edited by: David J Evans ]
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
What you have done is completely safe (well as far as the JTextAreas, I'm not sure what would happen if the user selected like 200 files ) but after each iteration you don't have a reference to each JTextArea created. Depending on what you are trying to do you may or may not need them. You could stick each one in a HashMap with the Key as the name of the file? Or just an ArrayList? Impossible to say what you want to do.
[ October 19, 2006: Message edited by: Tim LeMaster ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
just as an aside, you can replace all the commented out lines with just
a single line (indicated)

David J Evans
Greenhorn

Joined: Apr 28, 2006
Posts: 11
Tim, thanks for your suggestion, I'll probably experiment with sticking them into a hashmap like you suggest...just feel like I should maintain control over them in some way lol!

Michael, thanks for your shorthand - much less cluttered.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Originally posted by David J Evans:
Sorry Jesper, but I really don't see how what you said explained what I was seeing.

I'm sorry, I took a closer look and I see that I was confused.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by David J Evans:
just feel like I should maintain control over them in some way lol!


I think the most effective way to deal with it is to try to get rid of that feeling...

Seriously, to a beginner object oriented code often looks like there is some form of "magic" involved - things just seem to happen magically, without directly controlling it.

But that's actually a good thing - once you get rid of the need to control everything to the detail, and you begin to understand what's actually happening behind the curtains, what is making all that magic work - it will make you a much more effective programmer!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: this is working but i don't understand why!!