7 Replies - 392 Views - Last Post: 30 June 2020 - 12:30 AM Rate Topic: -----

#1 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

why am i forced to set the counter's number to final?

Posted 28 June 2020 - 07:54 PM

in the following piece of code, i am generating 10 buttons to assign them random numbers out of 10 without any repetition and keep track of the button's pressed value in a Textfield namely jtf

however when i try to set jtf by the setText() method to the then valid counter number which is namely i, but Eclipse tries to make i final or it won't compile.

What might be the reason behind that ?



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Shuffler extends JPanel{
	JFrame fra=new JFrame();int x;JButton ss;String str,str2;
	JTextField jtf=new JTextField("   ",8);
	ArrayList<Integer> a=new ArrayList();
	JButton set=new JButton();
	JButton butt[]=new JButton[10];int arr[]=new int[10];
	static int asd[]=new int[10];
	private int cnt;
	static int asdx[]=new int[10];
	//private int[] st=asd;
	public static void main(String[]args){
		for(int i=0;i<10;i++)asdx[i]=i;
		//	st=asdx;
		new Shuffler().  go();	
	}




	private void shuffle(int[] asd) {
		// TODO Auto-generated method stub
		while(true){
			cnt=0;
			System.out.println();
			for(int i=0;i<10;i++){
				asd[i]=(int)(new Random().nextInt(10));
				//	System.out.println(st[i]);
			}
			;

			for(int i=0;i<10;i++){
				for(int j=9;j>i;j--){
					if(asd[i]!=asd[j])cnt++;

					;				}



			}


			if(cnt==45)break;


		}
		for(int i=0;i<10;i++)System.out.print(asd[i]);
		System.out.println();
	}







	private void go() {
		// TODO Auto-generated method stub
		fra.setVisible(true);
		fra.setSize(500, 500);
		fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fra.add(this);
		for(int i=0;i<10;i++){
			butt[i]=new JButton();
			butt[i].setText(""+i);
			a.add((Integer)i);
			this.add(butt[i]);
		}
		;add(jtf);		    

		for(  int i=0;i<10;i++){
			butt[i].addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					//Collections.shuffle(a);
					shuffle(asdx);
					String  in=arg0.getActionCommand();
					int innn=Integer.valueOf(in);
					str2=""+a.get(innn);
					for(int i=0;i<10;i++){
						butt[i].setText(""+asdx[i]);

					}
 
                    { set=(JButton) arg0.getSource();
// THIS FOLLOWINF LINE OF CODE
 jtf.setText(""+i);
                   
					for(x=0;x<10;x++){
						ss=((JButton)(arg0.getSource()));
						String hjk=ss.getText();
						System.out.println(hjk); 
						//ss=(JButton) (arg0.getSource());
						//str=ss.getText();

						//break;
					}

					}
				//	jtf.setText(""+innn);
				}

			});


		}	


	}
}





the code part i am talking about


for(  int i=0;i<10;i++){
			butt[i].addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					//Collections.shuffle(a);
					shuffle(asdx);
					String  in=arg0.getActionCommand();
					int innn=Integer.valueOf(in);
					str2=""+a.get(innn);
					for(int i=0;i<10;i++){
						butt[i].setText(""+asdx[i]);

					}
 
                    { set=(JButton) arg0.getSource();
[size="6"][i][b]// THIS FOLLOWINF LINE OF CODE
 jtf.setText(""+i);[/b][/i][/size]
                   
					for(x=0;x<10;x++){
						ss=((JButton)(arg0.getSource()));
						String hjk=ss.getText();
						System.out.println(hjk); 
						//ss=(JButton) (arg0.getSource());
						//str=ss.getText();

						//break;
					}

					}
				//	jtf.setText(""+innn);
				}

			});


Is This A Good Question/Topic? 0
  • +

Replies To: why am i forced to set the counter's number to final?

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12761
  • View blog
  • Posts: 45,947
  • Joined: 27-December 08

Re: why am i forced to set the counter's number to final?

Posted 28 June 2020 - 10:05 PM

You are using an anonymous inner class for your ActionListener. Anonymous inner classes cannot reference local variables unless said variables have been declared as final.

Based on the way you used your braces, the problematic line of code is referencing the loop variable here:
for(  int i=0;i<10;i++){ // this loop variable
    butt[i].addActionListener(new ActionListener(){



You overshadow the variable i in the loop within the ActionListener. I don't know if your intent was to reference the loop variable i corresponding to the loop inside the ActionListener.
Was This Post Helpful? 1
  • +
  • -

#3 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: why am i forced to set the counter's number to final?

Posted 28 June 2020 - 11:07 PM

View Postmacosxnerd101, on 28 June 2020 - 10:05 PM, said:

You are using an anonymous inner class for your ActionListener. Anonymous inner classes cannot reference local variables unless said variables have been declared as final.

Based on the way you used your braces, the problematic line of code is referencing the loop variable here:
for(  int i=0;i<10;i++){ // this loop variable
    butt[i].addActionListener(new ActionListener(){



You overshadow the variable i in the loop within the ActionListener. I don't know if your intent was to reference the loop variable i corresponding to the loop inside the ActionListener.


thanks. may i also take "your" views ( i started another topic on this one. I would not intend to breach any rules so please delete as applicatory. )

the question was this

Hello,

When I create an object as such

Object obj=new Object();

there are only 9 methods available, it shows in Eclipse

but in the books they say there are eleven. ( wait X 3, notify, notifyAll, getClass, equals, toString, hashCode, finalize, clone )

finalize() and clone() methods do not exist for the variable obj

obj only has wait X 3, notify, notifyAll, getClass, equals, toString, hashCode methods available to it.

why can not I finalize() or clone a regular object Object ?

Thank you !
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12761
  • View blog
  • Posts: 45,947
  • Joined: 27-December 08

Re: why am i forced to set the counter's number to final?

Posted 28 June 2020 - 11:51 PM

If anyone would like to continue this discussion, let's do so in your other thread. :)
Was This Post Helpful? 1
  • +
  • -

#5 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: why am i forced to set the counter's number to final?

Posted 29 June 2020 - 02:38 AM

ok continuing with our current discussion

View Postmacosxnerd101, on 28 June 2020 - 10:05 PM, said:

You are using an anonymous inner class for your ActionListener. Anonymous inner classes cannot reference local variables unless said variables have been declared as final.

and why is that?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12761
  • View blog
  • Posts: 45,947
  • Joined: 27-December 08

Re: why am i forced to set the counter's number to final?

Posted 29 June 2020 - 08:07 AM

Because that is the design decision that was made.
Was This Post Helpful? 1
  • +
  • -

#7 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 832
  • View blog
  • Posts: 6,392
  • Joined: 25-December 13

Re: why am i forced to set the counter's number to final?

Posted 29 June 2020 - 08:20 AM

My concept of what happens: When a method is called, the JVM allocates space on the stack for local variables. When the method exits, that space is reclaimed and will be used by the next method or whatever. If a method in an anonymous inner class that is executing asynch with the method that created it and that is now gone, the space on the stack for the referenced local variable does not belong to that variable any more. Putting final on a variable must tell the JVM to put the variable somewhere that it can be referenced later as needed.

This post has been edited by NormR: 29 June 2020 - 08:21 AM

Was This Post Helpful? 2
  • +
  • -

#8 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: why am i forced to set the counter's number to final?

Posted 30 June 2020 - 12:30 AM

View PostNormR, on 29 June 2020 - 08:20 AM, said:

My concept of what happens: When a method is called, the JVM allocates space on the stack for local variables. When the method exits, that space is reclaimed and will be used by the next method or whatever. If a method in an anonymous inner class that is executing asynch with the method that created it and that is now gone, the space on the stack for the referenced local variable does not belong to that variable any more. Putting final on a variable must tell the JVM to put the variable somewhere that it can be referenced later as needed.

is there a comprehensive Java book explaining such things like this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1