I was just wondering, after programing in LUA (Roblox) for a while then switching to java, why are Java variables to rigid. For example, if in Roblox, we wanted to do the equivalent of System.out.println(“hello world”), we’d set System.out.println() as a variable, (System.out.println()=output) whenever you wanted to output to the consol, you’d say output(“hello world”). If you took this concept to a deeper level, why cant we put variables into objects names. for example this code wouldn’t work:
public class test{
public static void main(){
String hi="hello world";
JLabel hi = new JLabe("hi");
}}
So, I was wondering why don’t they have this in Java, is their a reason other than they just didn’t think about it?
A practical use for this would be to have a simple and quick way of making JComponents in a orderly fashion. We could use a array or list to make these, but if we were making a bunch of JButtons and wanted to make all the buttons have a different action listener, we could make another array for the action listener (could we?) or we could use the method suggested above but this time with a integer and say add 1 to the integer every time (make it in a loop) Example:
public class test{
public static void main(){
int theLabel=1;
while(true){
JLabel theLabel= new JLabel("hello world");
theLabel+1;
}}}
Again, I feel that this would be a great addition to java and am wondering why doesn’t this exist?
Basically, the answer is “because this is Java”. It was designed the way it is, and Java programmers like it because it is the way it is. If it had all the features you described – it would be some different language, but not the Java as we know it today.