Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.