3

I'm not a total noob to programming. I've coded in one language or another for 20 years. Just not in Java. My main language is Objective C on iOS and OS X.

I'm developing a java app and I'm trying to implement the MVC pattern that iOS enforces. i.e. my UI, data model are separate classes and a third class, a controller is the 'glue'.

What I'm trying to do is super easy in objective c, and I've searched google all night trying to find the equivalent pattern in java.

For example I have two classes. Class A and Class B. Class A implements main(). main instantiates Class B. I need Class B to be able to call instance of Class A's methods. I'n Objective-C Class B would have a pointer to Class A as an invar. So when Class A instantiates Class B, it sets said pointer to itself, so Class B now has a reference to Class A and can call its public methods.

But it seems I can't do this from Class A where it insatiaties Class B (in Class A main(). Netbeans complains about not being able to access a non static variable from a static method to something like that. It seemed main() needed to be static.

I'm thinking maybe I could add another class analogue to the AppDeleagte in Cocoa. So I'd have a main() in some other class and it would instantiate the MVC.

I don't get this apparent restriction about static. The only time i encountered it was with a local that needed to retain its value through successive method calls.

I never declared a method static, nor encountered any problems accessing any vars that would normally be in scope.

This seems so simple I feel I'm missing something obvious.

PS I'm using NetBeans 7.01 and I'm just diving into Java on my own. I've tried getting help Yahoo chat rooms, and from a friend on fb, but he had to run.

I appreciate any help anyone gives on this.

Thanks for reading this ramble, and have a good night.

1
  • 5
    This is not "need a bit of pointing in the right direction" about MVC, this is not understanding one of the basic features of the language, static scope. It seems you may be trying to run before you can walk. I would suggest going back and going through a good Java tutorial, or one of the 'Java for C programmers' sort of texts, to make sure you've got a solid grounding in the language. If you already know Objective C, this will be a bit tedious, but will at least be quick. Commented Jan 21, 2012 at 11:37

3 Answers 3

3

There's a couple of questions here, I'll try to break them down and answer at least some of them:

First, to be able to call instance methods of A, you need to provide the instance to B when its instantiated. The following example does exactly this:

public class A {
    public static void main(String[] args) {
        new A().run();
    }
    public void run() {
        B b = new B(this);
        b.run();
    }

    public void foo() {
        System.out.println("2.");
    }
}

public class B {
    private final A a;
    public B(A a) { this.a = a; }

    public void  run() {
        System.out.println("1.");
        a.foo();
    }
}
// Output:
// 1.
// 2.

The important difference here is that Java doesn't give you magically access to call stack or "parent" of the current execution target. This means that you really do need to piggyback everything forward to the new instances if you want to call them.

Secondly, as NetBeans pointed out, you can't access non-static members from static contexts. This is best shown with a bit of code:

public class A {
    public static void foo() {}
    public void bar() {}

    public static int i = 0;
    public int j = 1;
}

public class B {
    public static void main(String[] args) {
        A.foo();       // works because foo() is static
        A.bar();       // compile error
        new A().bar(); // works because now you're calling
                       // non-static instance's bar() method.

        // Pretty much the same applies to fields;
        A.i = 2;       // works
        A.j = 2;       // compile error, once again
        new A().j = 2; // works
    }
}

static in Java is pretty much an equivalent of a class global (which makes it a very dangerous keyword in OOP and quite honestly should be avoided at all costs). Almost everything else needs to be accessed in an OOP manner or in other words, through an object instance.

While having a global state for certain things might sound reasonable (such as application's configuration), in the long run it isn't - you're just as good creating a seemingly singleton object that contains configuration state but can be modified on the fly if needed; this way you can avoid restarting the application when the configuration changes.

I hope this helps and answered your questions and don't hesitate to ask even more if needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to everyone who replied, and especially Esko. I think this will get me where I need to be. Statics being like a class global would explain why I don't understand them in a Java context, since my programming experience in Objective-C has kept me to a strict OOP paradigm. I'm pretty sure they don't behave that way in ObjC, but I'm not sure as I've never really used them.
2

Declaring a method static means that the method is a method of the class, and not a method of the instance of that class. So, obviously, this has no sense in a static method: there is no instance of the class to which this could refer.

What you want is this:

public class ClassA {

    public static void main(String[] args) {
        ClassA a = new ClassA();
        ClassB b = new ClassB(a);
        ...
    }
}

Read the tutorial about static members.

2 Comments

That makes things clearer JB. Now do I have to pass instance a's reference to instance be in the constructor? In my specific case, Class A is a controller, Class B is my UI (a View in Cocoa parlance). Being a UI I made it in the NetBeans IDE and it won't let me even insert whitespace before a comment in any of its generated code. I can see how I was confused, because in objective-c, classes are really objects also and self has context for a class object, just as it does for instances.
NetBeans allows modifying the constructor of a view. Don't confuse the constructor with the initComponents method, which is just a regular method. You may also use a setter method to initialize the reference, but using the constructor looks like a better design to me. I would not use an IDE, and even less a wysiwig view editor, to learn Java. It will annoy you more than it helps you, and you'll miss important parts of developing and running Java applications.
2

Once you've got past this hurdle (as described by others) you'll see that the ethos of Java supports MVC development. There are even Observer and Observable interfaces that you can implement so that you can easily hook up networks of event-driven objects.

The whole of the Swing framework relies on MVC concepts too, so you'll have an underlying model that you can overlay multiple views on. (You can even consider building your GUI using the same framework that Netbeans is built on so you'd get a lot of reuse of mature GUI components.)

Java is a reasonably complex platform though (though the language is disarmingly simple itself). It's worth reading a book, or having some good reference material to hand before diving into this, to make sure you structure things properly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.