-
need suggestion about "new JRadioButton"
hello,
i'm new in java, and i need suggestion about things below:
which is better:
to use
Code:
private void try {
radiobutton1.setSelected(true);
radiobutton2.setSelected(false);
radiobutton3.setSelected(false);
}
or this one:
Code:
private void setSelectedRadioButtons(JRadioButton[] rb, boolean[] b) {
for (int i=0; i<=rb.length; i++) {
rb[i].setSelected(b[i]);
}
}
private void try {
setSelectedRadioButtons(
new JRadioButton[] { radiobutton1, radiobutton2, radiobutton3 },
new boolean[] { true, false, false }
}
}
currently, i'm using the latter in my program, but i'm afraid that it takes lots of memory or
it's a "not suggested" line of code.
if the latter is ok, then i will use it for other components and properties
thank you
Last edited by efm; 08-30-2006 at 05:29 AM.
-
The idea of the latter is better, because it is more reusable...it would support any number of items, rather than just 3, which you would have to recode for another program that needs more than three items. However, I don't think you should name a method 'try', as that is a Java keyword used in exception handling. Not necessarily an error, but it's bad practice.
-
Does this code compile for you, i dont see a ) to close the method.
private void try {
setSelectedRadioButtons(
new JRadioButton[] { radiobutton1, radiobutton2, radiobutton3 },
new boolean[] { true, false, false }
}
}
-
hi all,
major: i have a typo there, you're rite, it should be a ")", not a "}"
pirate: thanks for the suggestion, so it's ok if i use the second lines of code? will i get memory lack or
something? because i don't really understand of the keyword "new" is. isn't it to make a new instance?
and about that "try" keyword hehe, i wrote it myself, not copy pasting it from my code. so it's a
mistake.
thank you
-
 Originally Posted by efm
pirate: thanks for the suggestion, so it's ok if i use the second lines of code? will i get memory lack or
something? because i don't really understand of the keyword "new" is. isn't it to make a new instance?
It should be fine, you don't have to worry so much about the memory leaks in Java, because Java has automated memory management, unlike some other OOP languages. Any values you've set up in RAM will be cleaned out by the garbage collector a few milliseconds or so after no reference variables are pointing to them. The new operator is typically used to make an instance, such as:
Foo f = new Foo();
myMethod(f);
but, you can also use it to create objects on the fly without a variable, the way you are doing it with your code, such as;
myMethod(new Foo);
Last edited by piratepops; 08-30-2006 at 11:11 PM.
Reason: reducing quote clutter
Similar Threads
-
By ram_mcse in forum Security
Replies: 3
Last Post: 12-01-2006, 11:05 AM
-
By swapnil_paranja in forum C++
Replies: 0
Last Post: 06-23-2005, 07:29 AM
-
By wind_son in forum .NET
Replies: 0
Last Post: 06-13-2002, 12:41 AM
-
By ScottD in forum Architecture and Design
Replies: 0
Last Post: 05-24-2002, 06:03 AM
-
By Kamlesh Patel in forum Java
Replies: 6
Last Post: 07-09-2001, 08:13 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks