Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > method calling [writing methods cont.]

Reply
Thread Tools

method calling [writing methods cont.]

 
 
KyoGaSuki
Guest
Posts: n/a
 
      03-20-2008
First of all, you have all been so helpful, thank you n.n. Just one
more question though (sorry it is a new topic, but due to all the spam
posts, I thought posting it as a new topic would get me better
results). Can someone give me a basic (like...the most beginner-form)
example of calling a method? I think I have got down the basics of
writing a method, but I need a couple of examples of calling that
method.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-20-2008
KyoGaSuki <(E-Mail Removed)> writes:
>I think I have got down the basics of writing a method, but I
>need a couple of examples of calling that method.


(If replying, please do not quote this complete post, only
single lines you refer to.)

Let me try to explain how to call static methods with no
argument and with one arguments. (There also are non-static
methods, so maybe then someone else can explain how to call
non-static methods.)

We will start with

.----------
|A first Java program

The following program (3 lines) prints a so-called "stack dump".
The reader is not required to know, what a "stack dump" is,
it is used here just for the fact that it writes something.

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.Thread.dumpStack(); }}

Install the JDK, add the bin-directory of the JDK
to the PATH of the shell, write the programm into
a file »Hallo.java«, open a text command shell,
compile the program by the following command

javac Main.java

and then run it with

java Main

The output should look similar to

java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1206)
at Main.main(Main.java:3)

What is happening here?

»public class Main«

This specifies the name of our program as »Main«.
(Actually it is called a »class«, but in this case it
can be regarded to constitute the "program".)

The word after »class« needs to match the part
of the filename »Main.java« before ».java«.

»public static void main( final java.lang.String[] args )«

This introduces the section of the actual commands
and always needs to be written this way (or a very similar way).
More details about this will be given later.

»java.lang.Thread.dumpStack();«

This is a so-called "statement". It will be executed
after a process has been started that is being controlled
by the above program. When being executed, it will dump the
stack.

Exercise:

Compile and run the program as being described above.

Hints:

If the program should not run, the following
conditions can be checked:

- The student needs to know some fundamental aspects of
his computer and operation system before attempting to
learn Java. For example, how to rename a file, how to
use a command line shell, how to use the keyboard.
These topics are not treated here and needs to be learned
ahead by other means.

- All commands needs to be typed in exactly as spelt above.

- The source file must have the name »Main.java« needs to
reside in the current directory of the command shell.
No directory names should be written at the start of the
arguments of the commands.

- The environment variable »CLASSPATH« should not have been
set to any value. Otherwise, it needs to be cleared.

- Under Windows:

- The current directory should not be on a virtual drive
created by "subst" or be given by a UNC network path.

- The output of the command "set" should not contain
»CLASSPATH«, the output of the command »type Main.java«
should be the three lines of the program as given above.

Exercise:

Modify the program somehow and observe the behavior
of the compiler when it is invoked for the modified program.

.----------
|Expressions give an Action

Whenever someone wants to request something he has to designate
what it is that he requests. A program is such a designation.

The basic building block for designations is the expression.

A simplified default form for the expression is as follows.

head()

Such a head-parentheses-expression consists of a head and
parentheses.

Such an expression can denote an action, which is given by
its head. An action is just what the normal meaning of »action« in
the English language is, i.e., some activity.

In the program given above there is the following expression.

java.lang.Thread.dumpStack()

Its head is »java.lang.Thread.dumpStack«.
It denotes the action "dumping the stack".

The action indicated by the head will actually happen in
the moment when the expression is being »evaluated«.

This raises the question, how to request the evaluation of
an expression, which is answered in the next section.

.----------
|The expression-statement

To request the evaluation of an expression, the
expression-statement is used, which consists of an
expression and a semicolon.

»java.lang.Thread.dumpStack();« is an
/expression statement/.
It request the evaluation of the expression
»java.lang.Thread.dumpStack()«. The evaluation of this
expression will have the effect of the output of a stack
dump, as being explained in the previous section.

The general form of an expression statement is a semicolon
following an expression.

expression;

The execution of the expression-statement will
evaluate the expression. This will make the effects happening
that are specified by the head of this expression.

So, the execution of the statement
»java.lang.Thread.dumpStack();« evaluates the expression
»java.lang.Thread.dumpStack()«, and this evaluation will
dump the stack, because this is specified by the head
»java.lang.Thread.dumpStack«.

.----------
|Names qualified by a class name

The head of the expression »java.lang.Thread.dumpStack()«
is given by the qualified name »java.lang.Thread.dumpStack«,
ending in the simple name »dumpStack«.

In a large program, confusion is possible if simple names
are used, because one might want to used the same name
for different meanings. Also, one can not easily comprehend a
huge list of thousands of names.

Therefore, simple names are grouped into /classes/. Names
that belong together are grouped into the same class. Thus,
they can be found more easily and simple names that are
part of different classes might be the same.

»java.lang.Thread.dumpStack« is a qualified
name, consisting of the class name »java.lang.Thread« and
the simple name »dumpStack«.

.----------
|Names qualified by a package name

The class name »java.lang.Thread« is qualified itself.
It consists of the unqualified class name »Thread« and
the package name »java.lang«.

Unqualified /class names/ usually are started with an
uppercase letter.

There might be other classes
with the unqualified name »Thread« within other packages,
but »java.lang.Thread« refers to the class »Thread« of
the package »java.lang«.

Package names usually are started with a lowercase letter
and might contain dots ».«.

.----------
|Documentation of Names

The meaning of names is being explained in the JavaDoc.

For example,

http://download.java.net/jdk7/docs/a...tml#dumpStack()

Here is an excerpt from the above page:

java.lang
Class Thread
dumpStack()
Prints a stack trace.

This documentation is to be read as follows:

»java.lang«
This give the package of the class.

»Class Thread«
This gives the class.

»dumpStack()«
This gives the name »dumpStack()«.
(»public static void« will be explained later.)

»Prints a stack trace.«
A description of the effect of an evaluation of
an expression with this name as its head.

The qualified name »java.lang.Thread.dumpStack« and the simple
name »dumpStack« both then can be refered to as an »expression name«.

Exercise:

The documentation of »yield« is as follows.

java.lang
Class Thread
yield()
Causes the a very short (not noticeable) pause.

This documentation is a simplified and modified excerpt
from the following page.

http://download.java.net/jdk7/docs/a...ad.html#yield()

Answer the following questions by reading the above except:

- What is the class of the name being described?
- What is the package of this class?

Assignment:
Modify the program given above so as to activate an expression
with the name being described above
instead of the expression »java.lang.Thread.dumpStack()«.
When this is done correctly, no errors should be reported and
the program should print nothing instead of a stack dump.

Hints for the assignment:

- Start with the preceding example program that
printed a stack trace.

- Only change the last line of the program.

- Do not attempt to copy parts of the documentation
into the program. Instead read the documentation,
and the modify the program.

Remark:
The name being introduced in this exercise not actually
useful in Java programming, it can be considered obsolete.
But because it can be called quite simply, it still is used
here, just for the purpose of an example.

.----------
|White Space

When a word (usually made up from letters) is followed by
another word, both words need to be separated. Such a
separation can be done by a space. Thus, one writes
»class Main«, not »classMain«.

Instead of a space an arbitrary combination of multiple
spaces and newlines (line separators) can be used, that
includes at least one space or newline. Such a combination
is called "white space".

White space is not needed, but allowed, on the side of
special characters. Thus, the following lines are all
equivalent:

java.lang.Thread.dumpStack();
java . lang . Thread . dumpStack ( ) ;

One even is allowed to write this as follows.

java.


lang. Thread.dumpStack
() ;

White space must not be inserted within a word, so
»Thread« must not be written as »Thre ad«.

Exercise:

Insert and remove spaces and newline characters of
the above program to make it look as strange as possible
while still compiling and printing a stack dump.

.----------
|Comments

Wherever white space is allowed, a comments may be
inserted, that is essentially being ignored (»deleted«)
by the compiler.

A comment starts with »/*« and ends with »*/«.

public class Main /* This is a comment */
{ public static void main( final java.lang.String[] args )
{ /* Here is another /*
* comment */ java.lang.Thread.dumpStack(); }}

Exercise:

Remove all the comments from the above program, then
confirm that it still compiles and prints a stack dump.

Exercise:

Add some comments to the result of the last exercise and
confirm that it still compiles and prints a stack dump.
Finally, remove all comments, again.

.----------
|Printing lines

The expression »java.lang.System.out.println()« has the effect
to print a line.

This means an »empty line«, so it might be hard to spot
in the output, but might be visible in comparison to a
previous program using »yield« (in an exercise).

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println(); }}

The name »println« is not directly contained within
the class »java.lang.System« but within a part
of the class, which is called a »field«. The name of this
field is »out«. Therefore, here the precise location of
»println« is given by the prefix »java.lang.System.out«.

The simplified documentation of println reads:

println()
Write a newline character.

.----------
|Arguments

While the coarse meaning of an expression is given by the
head in front of the parentheses, finer details can be
specified as arguments within the parentheses.

To print the text »Hello!« the following expression can
be activated.

java.lang.System.out.println( "Hello" )

The text needs to be written in between quotation marks
to mark it as a text.

A Specification of an expression written within the
parentheses of the expression is called an »argument«.

The simplified documentation of println with an argument reads:

println( text )
The evaluation of this expression
writes the text given as the argument
and then writes a newline character.

Thus, the following program prints »Hello!«.

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( "Hello!" ); }}

This is the end of the explanation how to call static methods
with no arguments and with one argument. Coincidentally,
it did not actually use the word »method«, but still treated
how to call a method.

(If replying, please do not quote this complete post, only
single lines you refer to.)

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>Let me try to explain how to call static methods with no
>argument and with one arguments. (There also are non-static
>methods, so maybe then someone else can explain how to call
>non-static methods.)


To correct myself: I also showed how to call »println«, which
/is/ a non-static method. So I showed how to call static and
non-static methods in some simple cases, but there still is
more left to be said. For example, I only showed methods with
an effect and no result, while there also are methods with a result.

 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      03-20-2008
> results). Can someone give me a basic (like...the most beginner-form)
> example of calling a method? I think I have got down the basics of
> writing a method, but I need a couple of examples of calling that
> method.


Sun's tutorial has some good examples:
http://java.sun.com/docs/books/tutor...O/methods.html


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-20-2008
On Thu, 20 Mar 2008 13:08:09 -0700 (PDT), KyoGaSuki
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>Can someone give me a basic (like...the most beginner-form)
>example of calling a method? I think I have got down the basics of
>writing a method, but I need a couple of examples of calling that
>method.


You need an intro text for that sort of question. See
http://mindprod.com/jgloss/gettingstarted.html

There are hints on how to do this on the cheap.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-21-2008
KyoGaSuki wrote:
> First of all, you have all been so helpful, thank you n.n. Just one
> more question though (sorry it is a new topic, but due to all the spam
> posts, I thought posting it as a new topic would get me better
> results). Can someone give me a basic (like...the most beginner-form)
> example of calling a method? I think I have got down the basics of
> writing a method, but I need a couple of examples of calling that
> method.


I provided an example on the other thread.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design question - methods calling methods Rhino Java 49 05-29-2010 03:16 AM
calling class methods from class methods, help? Oltmans Python 6 03-11-2009 07:59 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
using dynamic method calling with attr_writer methods Markus Hohenhaus Ruby 3 05-09-2007 10:35 PM
Avoid calling non-final methods in a constructor: Applies to static methods too? Oliver Wong Java 14 06-13-2006 09:18 AM



Advertisments