finally question

import java.io.*;
import java.io.*;
class Base888{
public static void amethod()throws FileNotFoundException{}
}



public class ExcepDemo8 extends Base888{
public static void main(String argv[]){
       ExcepDemo8 e = new ExcepDemo8();
}

public boolean amethod(int i){
 try{
    DataInputStream din = new DataInputStream(System.in);
    System.out.println("Pausing");
    din.readChar();
    System.out.println("Continuing");
    this.amethod();
    return true;
    }catch(IOException ioe) {}
    finally{
    System.out.println("Doing finally");
    }
    return false;
  }

  ExcepDemo8(){
      amethod(99);
  }
}


I was trying above example from link
http://www.jchq.net/certkey/0204certkey.htm
did not understand output while running in eclipse ide.

I was trying to understand the output.
can we call like
   this.amethod();

within same amethod().

 Any links, ideas, resources,sample code highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
In this case you cannot call this.amethod in any situation,
because amethod is a static method - it should be preceded
by class name, not by instance name and this is in fact instance

it should be  Base888.amethod - this is how it can be called
0
for_yanCommented:

in more general terms you can
sometimes call a method from within itself - this is
called recursion and such thing is sometimes
very effective ofr programming special
kinds of operations, e.g. traversing different jkind
of trees where you don't know how many branches they will have.

However it is not always eaisy to understand the logic oif these operations
and it is very easy to end up in infinte loops ets.
So you should be cautios before you use recursive operations.
0
for_yanCommented:

Why do  you call this question "finally question" ?
This is not a question about "finally" operator.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

gudii9Author Commented:
for me above program executed strangely and gave output like

>>Pausing

Continuing
Doing finally


please advise
0
CEHJCommented:
Yes, after amethod(99) is called, the finally block is bound to be executed, before the method returns.

Incidentally 'finally' is a keyword, not an operator
0
CEHJCommented:
>>executed strangely

Why is that strange?
0
gudii9Author Commented:
I said strangely based on previous post

>>>>In this case you cannot call this.amethod in any situation,
because amethod is a static method - it should be preceded
by class name, not by instance name and this is in fact instance

it should be  Base888.amethod - this is how it can be called




Even though i did not call static method like
Base888.amethod

instead i called like
this.amethod


still it worked fine.

My main question is can we call method XYZ() inside methodXYZ() as this.methodXYZ()/classname.methodXYZ()
please advise
0
CEHJCommented:
Yes it will work . It's *better* to  call X.foo than just foo where foo is a static method though

>>My main question is ..

Yes you can
0
for_yanCommented:

Please, do not confuse two questions.

One about static methods and instance methods.

Static method is general class method - it will run the
same way for any instance of the method as it cannot use instace
variables - it can use only imput paramteres, local variables (living only insiade method)
and constants. As it does not use instance varaibles you precede such methods
with the class name, not with the variable representing the instance.

this. represents the instance - this instance, current instance of your class.
And you cannot and should not prefix static methods with
instance no matter if in some special
cases you are not getting errors - don't do it - it creates confusion.

Static methods should be preceded by cass name if you execute them
from within the class different from where
they were defined.  If you execute them within the same calss
they should not be precede by anything.

Instance mnethods should be preceded by variable representing instance
(this is a special case of such instance).


Wherther you can or cannot call the method from within itself (recursivity)-
this is a special issues orthogonal (not cnnnected logically)
to the idea of static or instance methods (see about
recursivity in my post above) and please try  not to mix these
two issues for now.
Recusrivity is something which you may learn later - I suggest not to use it in the
beginning of your programmimng in Java, howvere
static vs non-static methods - is a part of genaral understanding
of java which you need have full and clear understanduing
 
0
gudii9Author Commented:
>>>Instance mnethods should be preceded by variable representing instance
(this is a special case of such instance).

when you mention this is a special case which one are you referring. also what is recursivity. please advise
0
for_yanCommented:
I meant that normally instance methods are invoked by attaching
dot and method name to the instance of the class.

"this" is a special word which respresents the instance
of the  class to which this piece of code belongs - this current instance.

Therefore this.method() will execute method() using instance variables
of the current class.
In general people in most cases will not write

 this.method();
 as it is equivalent

simply to

 method();

Howvere to write this.method() will not be a mistake - it is just equivalent to method()

If amethod() is a static method then to write this.amethod() is
 against the logic - static method does not apply to instance, it applies to
the whole class. The right way will be to write

Class_name.amethod()

if you have say

public class MyClass {

int num;

public MyClass(int num){
this.num = num;
}

double myMethod(){
//do something, priobably with value num
}

static double myStaticMethod(int num1){
//do something with num1; cannot do anything with num in this method
}

 }

then to use this class in another java program
you'll say

MyClass mc = new MyClass(5);  // create instance

mc.method(); //execurte method on this instance after you created instance

if you want to execute static method you don't need to create
instance , you just say:

MyClass.myStaticMethod(5); // you attach this method not to an instance but to name of the class




Recursive method has nothing to do with the Static vs Instance  methods
discussion above; it  is a  special case of method
which at some point of its execution calls itself.
If there are no formal requirements - if I were you at this moment
I would suggest that you forget about this recursivity at least for now.
You can program in Java for years and never
use a single recursive method. There are tasks (as I mentioned above)
were such methods allow to make program smaller and more effective
but such times are really relatively rare.

on the other hand disticntion between instance and static methods is fundamental to java
and needs to be understood before you would want to write Java code or read
someone's Java code.  

You can read abourt Static vs Instance methods for example
in the document below, although it may be a little bit
longer text than you'd  want.

http://www.cs.berkeley.edu/~kamil/teaching/sp03/020303.pdf






0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
i will read it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.