1

From my understanding, this is an example of variable declaration:

int variable;

and this is an example of variable initialization:

variable = 2;

Together, we can both declare and initialize a variable as such:

int variable = 2;

For methods, we have similar constructs:

abstract int method(int param);

and

int method (int param) {
return param;
}

but instead we call the former a method prototype and the latter a declaration.

Edit:

Another example would be as follows:

static int differentMethod(int param);

public static void main (String[] args) {
...
}

static int differentMethod(int param) {
...
}

Edit 2: Ignore the example immediately above as it's not supported in Java (but something similar exists in C).

Is there such a thing as method initialization?

PearSquirrel
  • 595
  • 2
  • 7
  • 17
  • 2
    From your point of view, where and when should the method initialization be called, in case it exists? – Luiggi Mendoza Sep 01 '14 at 06:31
  • Well I'll edit my post but there's also the case when you declare the method at the top of your class in order to define it later on. I suppose when you define the body of the method that would be considered the initialization of it. – PearSquirrel Sep 01 '14 at 06:33
  • read about overload, abstract, interface, orverride... – Yazan Sep 01 '14 at 06:34
  • Your example works in C. And Java is not C and doesn't support that kind of design. Probably with interfaces, but this's not allowed in Java. – Luiggi Mendoza Sep 01 '14 at 06:36
  • @PearSquirrel: *"...there's also the case when you declare the method at the top of your class in order to define it later on."* Not in Java. In Java, that's neither necessary nor possible. – T.J. Crowder Sep 01 '14 at 06:36
  • @Yazan: From my understanding method overloading is used to differentiate different methods with the same name (but different parameter lists) and isn't really related to initialization. Overriding is definitely closer. I guess it's just the differences in terminology between methods and variables. – PearSquirrel Sep 01 '14 at 06:39
  • the point is , using abstract method is done on purpose, for a specific reason, by the designer/developer, its not necessary, its related to object oriented – Yazan Sep 01 '14 at 06:41
  • @LuiggiMendoza: Whoops, I guess I mixed the two up. – PearSquirrel Sep 01 '14 at 06:41
  • in Java it not depends where your method is located in the top of the class or the bottom. So you cannot declare it somewhere and then initialize it. – Nadir Novruzov Sep 01 '14 at 06:41

4 Answers4

1

All methods in java are contained within a class, so when a class is loaded its methods are also loaded into memory. There is a section called PermGen that stores all methods( static methods too and static variables), PermGem is part of Heap.

The method prototype that you refer to is used in interfaces and abstract classes. Note that it is compulsary to implement those methods in the concrete class,since conrete classes are the ones that has running conde in them, when that class will be used in code(initialized) its methods will be loaded into memory for execution.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • I think a good way to think about the situation is to compare classes as a whole to variables instead of only methods to variables. primitive types, like classes, have both state and behavior, although their functionality is more simple. The behavior is defined at declaration (like class declaration and, in turn, method declaration), and their state is determined at initialization (like object initialization). When I think of it this way, everything makes a lot more sense. – PearSquirrel Sep 01 '14 at 07:23
  • Classes are complex data types, thats true. But your statement `primitive types, like classes, have both state and behavior` is wrong. primitives are just named memory spaces, which are used to store data of a particular type. They do not have methods, you are confusing them with their respective wrapper types, which are used to represent primitives in form of objects eg:- `Integer` for int, they are used where pure primitives cannot be used, for example in collections. – Mustafa sabir Sep 01 '14 at 07:36
0

Interesting. Theoretically, there IS method initialization.

Method initialization could mean treating the methods as first class objects and in the runtime, maybe, assign a particular value to them. Yes, you are initializing the method to one and later, you are changing it to another. Just like an object.

Objective-C has blocks. C has function pointers.

In java, there is a partial support for such through anonymous inner classes.

Here is a wikipedia article about it: http://en.wikipedia.org/wiki/First-class_function

avismara
  • 5,141
  • 2
  • 32
  • 56
  • 1
    Yeah, I agree that there is behavior for defining the body of a method later on (through anonymous inner classes). I think that we don't talk about method initialization or assignment because they aren't nearly as intuitive as variable initialization or assignment. The functionality may exist but it's easier to name them something else. – PearSquirrel Sep 01 '14 at 07:18
0

Variables are stored in memory, that is, they have some value and that value is stored at some memory address. Note that : that value CAN be later changed by us. Now let us see methods,each method also is stored somewhere. When we invoke that the method, its address is resolved and then the instruction stored at that address is executed. BUT, as we change the value of the variable, note that the value of the address at which the method is stored cannot be altered by us. It is internally handled. Hence, there is no such thing as INITIALIZATION of methods.

Sagar D
  • 2,588
  • 1
  • 18
  • 31
  • Interesting. It makes sense to use the word 'initialization' in the context of something that is likely to change, e.g. variables. You set the variable to some initial value and later on can assign different ones. I suppose my question warrants the follow-up question, 'is there such a thing as method assignment?'. My conclusion would be 'no', because methods and variables have a number of similarities in syntax but don't have a one-to-one relationship. – PearSquirrel Sep 01 '14 at 06:54
  • Yes, there isn't anything called as method assignment too. All you can do is method declaration, then its actual implementation, then call it. Think upon it more practically, would `initialization` and `assignment` actually serve any use regarding methods? – Sagar D Sep 01 '14 at 07:02
  • My conclusion was no, they don't, because, at least as far as I'm aware of, methods don't store values that can be changed at runtime. (although static final variables can be initialized, albeit only once... I guess it's because any code that actually can run won't have a method without a body.) – PearSquirrel Sep 01 '14 at 07:09
  • Yes you are absolutely correct, methods are blocks of statements that optionally operate on variables, but the statements contained in the methods itself cannot be changed at runtime. Hence, to conclude, initialization and assignment are not possessed by methods. :) Hope this clears. – Sagar D Sep 01 '14 at 07:14
0

You can use concept used in What's the nearest substitute for a function pointer in Java?

So you will get something like method pointer that can be initialised later. You can't have pointer to function in java so you must wrap it in an object.

EDIT: example

    interface IntFunction {
        int function(int param);
    }

    public IntFunction method;

    void initializer() {
        method = new IntFunction() {

            @Override
            public int function(int param) {
                return param + 10;
            }
        };
    }


    void use() {
        int b = method.function(50);
    }

EDIT 2: direct bytecode manipulation

You can use bytecode manipulation technique at runtime, like javassist.org do, so you can do wethewer you want with your class e.g. initialise your method at runtime, but it is not so clean way.

Community
  • 1
  • 1
j123b567
  • 3,110
  • 1
  • 23
  • 32
  • Cool! Still, that's called 'using anonymous inner classes', not initializing a method. I guess we can recreate the behavior but there is not specific functionality that we can call 'method initialization'. – PearSquirrel Sep 01 '14 at 07:14
  • You can use bytecode manipulation, as described in "edit 2" of my answer. – j123b567 Sep 01 '14 at 07:53