Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
why those values cannot be seen in the main class??? pls Posted by lwiq on 26 Apr 2007 at 1:20 AM
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();
}
}
Report
Re: why those values cannot be seen in the main class??? pls Posted by zibadian on 26 Apr 2007 at 3:01 AM
: 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:
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;
     }

}

In your main() you can now get and set the values like this:
      Aaa a1 = new Aaa(m,n);
      m = a1.getI();
      n = a1.getB();
      // Example of set:
      a1.setB(n);



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.