开发者

java constructor function newbie question

Im newbie in Java and im learning it.

Right now i have two class, i already called class B on class A constructor

class A
public A {
init();
B bb = new B(textField);
bb.doSomething();
}

void init() {
    textField =  new JTextField();
}

void fly() {
//can i just开发者_运维技巧 use bb.doFly();
}

private JTextField textField;
}

My question is, do i have to initialized B bb = new B(); on every single function i want to use on A ?

Question Edited


Its all about scope. If your attribute is at class level, then no you do not have to create a new one each time. If the attribute is only created within a method scope, then yes you do.

The difference is

class A {

   public A() {
       // this is at method level scope. It is define INSIDE a method
       B bb = new B();
   }
}

class level scope is

class A {

   // this is at class level scope. It is define OUTSIDE a method
   B bb = new B();

   public A() {

   }
}


No, just refer to the identifier bb:

public class A {
    private B bb = new B;

    public A() {

    }

    public void foo() {
        bb.printHello(); // you can only call methods on bb which class B supplies
        bb.fly();        // won't work; you can only call fly() on objects of type A
    }

    public void bar() {
        bb.printWorld();       
    }

    public void fly() {
        System.out.println("I'm flying...");
    } 
}

public class B {
    public B() {

    }

    public printHello() {
        System.out.println("Hello");
    }

    public printWorld() {
        System.out.println("Hello");
    } 
}

Just a note: Please declare all your attributes private unless you have a good reason to not to.


class A
B bb;
public A {
bb = new B();
bb.doSomething();
}

void fly() {
bb.something()
}
}


No. Suppose you have this code this code:

public class NameOfProgram
{
    public static void main(String[] args)
    {
        A aa = new A();
        aa.fly();
    }
}

Then the variable bb inside aa is created with the statement new A(). So, you don't have to create an B object and set it to bb as it already exists.

Also, you need to declare the variable outside the constructor or you will not be able to use it in other methods, like fly. To correct it you could do this:

public class A
{

    B bb;  <-----------------

    public A()
    {
        bb = new B(NameOfVariableIForgotAbout);
    }

    public void fly()
    {
        bb.doFly() //Now this should work
    }
}

you can also but the B bb; after the method, I just prefer it this way.

Also, your class A has a few errors (so you note for future reference). Firstly, if you want to be able to call a method from an instance of the class (A aa = new A(); aa.fly()) then you need to make the methods public like so:

public void fly()
{
    //Insert code here
}

Also , you declared the constructor wrongly. Here is how you did it:

public A
{
    //Insert code here
}

and it is done like this:

public A()
{
    //Insert code here
}

The first error will cause a compile-time error if you make the call aa.fly() because it is neither private nor public.

The second will cause a compile-time error even if you make no call to the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜