Results 1 to 9 of 9
  1. #1
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default change value in the in the creator class

    How do i change value in the creator class
    for example:

    public class A {

    private int num = 0;
    private B b;

    public A(){
    b = new B();
    System.out.println(num);
    }
    public void changeInt(int newNum){
    num = newNum;
    }
    public static void main(String[] args) {
    A a = new A();
    }
    }

    public class B {

    public B(){
    A.this.changeInt(5); <-------- Error
    }

    }

    Error: No enclosing instance of the type A is accessible in scope

    thanks
    Last edited by livnihai; 10-03-2009 at 01:29 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read the classes section of Sun's tutorial.
    That line doesn't make any sense.
    Java Code:
    A a = new A();
    creates an A.
    Java Code:
    a.changeInt(5);
    changes the value.

  3. #3
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    ok,
    i'll explain my problem.

    I've created a class that listen to event, i need that when the
    event happen the main class sould know about that.
    I don't want to ask over and over from the main class
    The question is, how do i comunicate back from the secondery class
    to the main class

    thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    Isn't there a direct way to do it without third party ?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by livnihai View Post
    Isn't there a direct way to do it without third party ?
    What do you mean by "third party"? No third party is needed here. You can roll your own observer / observable interfaces (I do as I don't like the ones provided by Sun).

  7. #7
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    Any help how to implement Observer pattern
    maybe a simple tutorial

    thanks

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Go thru this article.Really ,it is simple and good.

    Abhi On Java: Implementing Observer Pattern in Java
    Ramya:cool:

  9. #9
    livnihai is offline Member
    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Default

    How do i use observer pattern on this simple example:

    [code]

    public class A {

    private int num = 0;
    private B b;

    public A(){
    b = new B();
    System.out.println(num);
    }
    public static void main(String[] args) {
    A a = new A();
    }
    }

    public class B {

    private int num;

    public B(){
    num = 10;
    }
    }

    I need that class A will print 10

    thanks

Similar Threads

  1. can we change label from action class?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2009, 02:12 PM
  2. Gui Swing Creator
    By 2o2 in forum New To Java
    Replies: 4
    Last Post: 10-01-2008, 11:38 PM
  3. Replies: 0
    Last Post: 06-25-2008, 10:33 AM
  4. Java Studio Creator - Bd Alterar
    By wagnerbarbos in forum JCreator
    Replies: 0
    Last Post: 06-09-2008, 05:19 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •