Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaLanguage
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Methods=== ['''public'''|'''private'''|'''protected'''] ['''static'''|'''abstract'''|'''final'''|'''native'''|'''synchronized'''] <Type>|'''void'''|<Type>'''[]''' <MethodName>'''('''<Parameterlist> ['''...'''<ArrayName>]''')''' ['''throws''' <Exception>{''','''<Exception}] {...}; Für den Namen der Methode gibt es keine Beschränkung. Er kann auch wie die Klasse heissen und wird vom ctor nur durch den return type unterschieden. There can be only one method with the same signature. It is also possible to overload a method by using the same name and different parameters. Calling a method is called invoking a method of an object or sending a message to an object. All parameters are passed by value. The value of object parameters and arrays is a reference. To manipulate parameters you have to use objects or arrays. A method can never change the values of their parameters z.B. die Referenz umhängen, so daß diese Änderung den Methodenaufruf überdauert. Methods that change instance fields are called mutator methods and those that only access instance fields are called accessor methods. <Parameterlist> := <Standardlist>|<Type>'''...''' <Varname> Since Java 5 the second is a '''variable argument list'''. <Varname> is always an array and can be used e.g. with the extended for. Beim Aufruf einer Methode kann der return value ignoriert werden d.h. man muss den return value im Code keiner Variablen zuweisen: A.DoSomething(); // der return value von DoSomething() wird ignoriert There is a this member available in every method. The this member can be used to discriminated between a parameter and an attribute e.g. void <Operation>(int x) { this.x = x; } or for ctor calling. There is also a super member to access members or methods of the super class with '''super'''.<MethodName>'''('''<Parameter>''');''' but it is not possible to call the method an indirect base class. ====Static methods==== Static methods or class methods do not operate on objects. You can use them without creating an instance of the object like <ClassName>'''.'''<StaticMethodName>'''('''<Parameter>''');''' ====Final methods==== Final methods can not be overwritten, d.h. Methoden in direkten oder indirekten subclasses mit der gleichen Signatur werden vom Compiler abgelehnt. Reasons can be efficiency (inline) and security (you know what you call). ====Abstract methods==== Abstract methods don’t have a body. If there is at least one abstract method the class must also be declared abstract. ====Native methods==== A method can be declared native, in which case the method is implemented in a platform-dependent way, for example, in C or assembly language. Because the implimentation is not provided by Java Language code, the method declaration contains no body (a semicolon appears instead of a block). Native methods are otherwise like normal Java methods; they are inherited, may be static or not, may be final or not, may override or be overridden by non-native methods, and so on. ====ctors==== ['''public'''|'''private'''|'''protected'''| <ClassName>(<ParameterList>) { ['''this('''<ParameterList>''');'''|'''super('''ParameterList''');'''] <statements>} } A ctor has no return value and it is not allowed to use the return statement with an argument. The compiler decides depending on the parameters which base class ctor is used. If a default ctor (without parameters) is missing, the compiler adds on. If an explicit ctor call for the superclass or for another ctor is missing ‘super();’ is inserted implicitly at the first line, so all ctors are chained. If the base class does not have a ctor without an argument this will lead to a compile error. A call super ( ... ); to a superclass constructor, whether it actually appears as an explicit constructor call statement or is implicitly assumed, performs an additional implicit action after a normal return of control from the superclass constructor: all the instance variables that have initializers are initialized at that time. More precisely: for every instance variable declared in the class containing the call, taken in the order in which the instance variables appear in the class declaration: if that variable has an initializer and either the initialization expression is not a constant expression or its value is not the standard default value for the variable, then the initialization expression is executed and its value is assigned to the instance variable. It is a compile-time error for instance variable initializations to have a forward dependency. Wenn man fälschlicherweise eine Methode mit gleichem Namen wie die Klasse und einem return type definiert, so handelt es sich um eine normale Methode und nicht um einen ctor. Private ctors verhindern, dass eine Klasse angelegt wird (s. Example (w)). A special ctor feature is the possibility to call a different ctor by ctor1(a,b,c) { '''this'''(new X(), a, b, c); // it must be the first line of a ctor } which also must be the first statement in the ctor. A call this(... ); to another constructor in the same class does not perform the mentioned additional implicit action. Bei ctor design ist generell zu beachten, daß subclasses damit zurechtkommen. Man ruft am besten keine privaten Methoden auf, und schreibt am besten immer auch einen parameterlosen ctor und eine Menge von u.U. private Methoden, die die Einzelschritte implementieren (s. 24 page 26). ====Finalize==== Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's finalize method. This process is known as ''finalization. ''The finalize method must be declared as follows and it is good practice to apply the following pattern: protected void finalize() throws Throwable { try { <statements> } finally { super.finalize(); } } There is a great difference between the C++ dtor and the finalize() method in java. The dtor is called when an object is deleted. The finalize() method is called by the garbage collector, so it might be called never. Finalize() methods are not chained automatically but have to be chained explicitly. There the last statement should be <code>super.finalize();</code>. In einem standard java program finalize is not called for the object with the main method (logisch but Java 1.1 had added a static method called runFinalizerOnExit() in the system class that will guarantee the the finalize method is called before java shuts down. Remember that you do not know when it is called.
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width