-1

I want to change a boolean attribute, based in main class, through a constructor. This is the code:

public class NewJDialog{

    public boolean CHECK;
    java.awt.Label label;
.
.
.
    Trasporta valore = new Trasporta(CHECK,label);
.
.
.
}

Class2

public class Trasporta implementsMouseListener,MouseMotionListener{



public  boolean value;

java.awt.Label label;



public Trasporta(boolean value, java.awt.Label ... pns){

    for ( java.awt.Label genericlabel : pns){
         genericlabel.addMouseListener(this);
         genericlabel.addMouseMotionListener(this);             
    }

   this.value=value;
}




@Override
public void mousePressed(MouseEvent e) {


    this.value = true;
}

In this way, CHECK attribute will not become "true".

Can you tell me why? How can I fix it?

Thanks in advance for your reply.

Biffen
  • 6,249
  • 6
  • 28
  • 36

1 Answers1

-1

You declare CHECK as primitive type, and not initialize it so it will have default value (primitive boolean are initialized as false)

Change your code to

Trasporta valore = new Trasporta(true,label);

if you want to initialize Transporta class with value = true;

Also, you should read this paper about naming convenction in java https://www.oracle.com/technetwork/java/codeconventions-135099.html

Wild_Owl
  • 431
  • 2
  • 7
  • that is not answering the question, OP wants to change the value (i`CHECK`) from another later called method (`mousePressed`) - I suppose – user85421 Apr 25 '19 at 17:18