Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

why those values cannot be seen in the main class??? pls

lwiqlwiq Posts: 15Member
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*;


class aaa
{
int i;
boolean b;
public aaa(int j, boolean c)
{
i=j;
b=c;

j=4;
c=true;
}
}

public class frame4b extends JFrame
{


public frame4b(String titlu)
{
super(titlu);
}

public static void main(String args[])
{
int m=1;
boolean n=false;
frame4b aplicatie = new frame4b("hello");
System.out.println(m+" "+n);
aaa a1 = new aaa(m,n);
m=a1.i;
n=a1.b;
System.out.println(m+" "+n);
aplicatie.setSize(300,300);
aplicatie.show();
}
}

Comments

  • zibadianzibadian Posts: 6,349Member
    : import java.awt.*;
    : import java.awt.Event.*;
    : import javax.swing.*;
    :
    :
    : class aaa
    : {
    : int i;
    : boolean b;
    : public aaa(int j, boolean c)
    : {
    : i=j;
    : b=c;
    :
    : j=4;
    : c=true;
    : }
    : }
    :
    : public class frame4b extends JFrame
    : {
    :
    :
    : public frame4b(String titlu)
    : {
    : super(titlu);
    : }
    :
    : public static void main(String args[])
    : {
    : int m=1;
    : boolean n=false;
    : frame4b aplicatie = new frame4b("hello");
    : System.out.println(m+" "+n);
    : aaa a1 = new aaa(m,n);
    : m=a1.i;
    : n=a1.b;
    : System.out.println(m+" "+n);
    : aplicatie.setSize(300,300);
    : aplicatie.show();
    : }
    : }
    :
    If you are referring to i and b, then they cannot be seen by other classes, because they don't have a public getter/setter method. Here's how your aaa class should look like in "correct" Java:
    [code]
    class Aaa {

    private int i;
    private boolean b;

    public Aaa(int j, boolean c) {
    i = j;
    b = c;
    // The following codes do nothing, because the changes in
    // j and c are not returned to the caller
    j = 4;
    c = true;
    }

    public int getI() {
    return i;
    }

    public void setI(int i) {
    this.i = i;
    }

    public boolean getB() {
    return b;
    }

    public void setI(boolean i) {
    this.b = b;
    }

    }
    [/code]
    In your main() you can now get and set the values like this:
    [code]
    Aaa a1 = new Aaa(m,n);
    m = a1.getI();
    n = a1.getB();
    // Example of set:
    a1.setB(n);
    [/code]
Sign In or Register to comment.