开发者

Java methods flow

I'm trying to learn how to program on android which of course uses Java. I understand Java vaugely, but this confuses me.

A method which I view as a function (PHP being my native programming language) can seemingly be declared anywhere 开发者_Go百科in a java file and still be pulled out at any other point is this so? What I mean is in PHP you have to define a function(method) to then be able to call it. So everything has to be in order.

Also is calling a function like including that section of code in your method calling it. Example being:

method 1 contains opendb command method 2 contains closedb command oncreate method calls method 1 then 2 does it act accordingly.

Sorry may sound dumb but I like concrete answers and not assumptions of mine.


The order in which methods are declared in java is of no importance.

Methods have no relationship to each other. You could invoke a method1 any number of times regardless of another method method2.

A sample could look like:

public DatabaseManager {
    public void openConnection() {
        // ...
    }

    public void closeConnection() {
        // ...
    }
}

Which you can invoke using:

DatabaseManager db = new DatabaseManager();
db.openConnection();
// do something
db.closeConnection();


A method which I view as a function (PHP being my native programming language) can seemingly be declared anywhere in a java file and still be pulled out at any other point is this so?

Well, partially true :-). Java (like many other languages) has the concept of "visibility" of a method (functions are usually called "methods" in Java). If a method is private, it is only visible (and usable) inside the same class, if it is public is can be called from anywhere. See e.g. the excellent Java tutorial, which covers this: http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

However, unlike PHP, the order in which methods are declared inside a single class is irrelevant. You can call a method from the same class before the point/line of its definition.

Also is calling a function like including that section of code in your method calling it. Example being:

Yes, in the simplest case calling a method behaves like including that code in the place where the method is called. But there are important differences:

  • If you call a method, it cannot access local variables from the calling method, and class fields only if both methods are in the same class.
  • Java (being object-oriented) has polymorphism: You call a method on an object instance, and the method that is actually executed depends on the runtime type of the object instance, which may be different for different code paths. In that case calling a method is more complicated than just replacing it with the method's code.
  • Java methods can recursively call themselves; that wouldn't work if the compiler just included them where they are called.

So it's probably not really helpful to think of method calls as "like including the code"...


Your problem is not Java at all. It sounds like you have never programed the object oriented way. You should learn what a class is, what a method is.

I strongly recommend the basics for OOP itself: http://download.oracle.com/javase/tutorial/java/concepts/


In Java, your methods can be declared in any order in the class, for example

class A {
    void C() { }
    void B() { C() }
}

Could be equivalently declared as

class A {
    void B() { C() }
    void C() { }
}

Your second question is not very clear. But just to clarify - there is nothing like including in Java to execute another script - generally you will create new objects or run static methods of a class to accomplish things.


Max... I have not coded in PHP, but I have done a lot of scripting. According to the online docs that I found, PHP does not natively support events. So you are right, everything has to be "in order" in PHP. So you may need to get your head around moving from the sequential model of programming that I grew up with, and move to the event driven model used in Android Java.

You can divide your program into three parts. A view or presentation (main.xml). The controller or event handler (MyApp.java) and the algorithms, say model.java. MyApp.java has "event handlers" that basically sit around waiting to receive events so that you cannot absolutely know the order in which methods will be called. Do the heavy work in Model.java and write it so that it knows nothing of the view and is reusable.

So in UNIX model is the ENGINE and the view and controller is the INTERFACE. INTERFACE-ENGINE vs model-Controller-view.

Hope that helps, JAL

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜