aspose file tools*
The moose likes Beginning Java and the fly likes Behaviour of return() ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Behaviour of return() ???" Watch "Behaviour of return() ???" New topic
Author

Behaviour of return() ???

achana chan
Ranch Hand

Joined: Jul 29, 2002
Posts: 277
Hi , I am new to this forum and to JAVA.
I need to understand a few basic behaviour JAVA class codes :
(1) I have a piece of code with both public and private methods : do I assume right that the public methods can be called from other JAVA applications through "import" ?
(2) In this class-file , there is this method :
public String toString(){
return "Name : " + v_name + "\n" + ... ;
}
Even though this method was not explicitly "called" from the JAVA application which inherited them, it displayed the results of the db queries. Do I assume right that this is due to the "return" JAVA command ?
(3) Will a JAVA application e.g.

work through all its methods (as distinct from a class-file, which will only use the method() called upon ?
(4) Using package, I seem to inherit everything in the subdirectory specified in the package statement e.g. package thisDemo seems to allow me to pull in all the classes in the subdirectory .../myDemo
(5) Similar to (4), I can "import myDemo.*" can I can call on all public methods, so what is the fine differences.
Finally, sorry for these basic questions, I grew up with 3GL's
[ August 23, 2002: Message edited by: achana chan ]

humanum errare est.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I am new to this forum and to JAVA.
Welcome to JavaRanch!
Here is a list of free Java tutorials that I have found useful:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials

  • (1) I have a piece of code with both public and private methods : do I assume right that the public methods can be called from other JAVA applications through "import" ?
    No. Import statements tell the compiler where to find classes and interfaces, not methods of classes or object.
    (2) ... and (3)
    I don't think that I understand the questions.
    (4) Using package, I seem to inherit everything in the subdirectory specified in the package statement e.g. package thisDemo seems to allow me to pull in all the classes in the subdirectory .../myDemo
    Perhaps there is a typo in the question - "thisDemo" versus "myDemo". And I don't think that I understand the question.
    (5) ... I can "import myDemo.*" can I can call on all public methods, so what is the fine differences.
    You cannot call the methods of classes or objects by importing a package. See the answer to question number one.


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    Originally posted by achana chan:
    (1) I have a piece of code with both public and private methods : do I assume right that the public methods can be called from other JAVA applications through "import" ?
    (2) In this class-file , there is this method :
    public String toString(){
    return "Name : " + v_name + "\n" + ... ;
    }
    Even though this method was not explicitly "called" from the JAVA application which inherited them, it displayed the results of the db queries. Do I assume right that this is due to the "return" JAVA command ?
    (3) Will a JAVA application e.g.

    work through all its methods (as distinct from a class-file, which will only use the method() called upon ?
    (4) Using package, I seem to inherit everything in the subdirectory specified in the package statement e.g. package thisDemo seems to allow me to pull in all the classes in the subdirectory .../myDemo
    (5) Similar to (4), I can "import myDemo.*" can I can call on all public methods, so what is the fine differences.

    (1) Yes.
    (2) Any Java object inside System.out.println() will call its toString() method. That's probably what happened.
    (3) Code inside main() will be executed unless it programmatically exits or encounters a runtime error ("exception").
    (4) No. Importing a package will not import its subpackages. You must import each subpackage explicitly.
    (5) Can you clarify? Fine differences between what and what?
    -------
    Domine absolvi
    achana chan
    Ranch Hand

    Joined: Jul 29, 2002
    Posts: 277
    I mean the difference between "package..." and "import ..."
    In one class file I have this (which is actually a sub-directory .../myDemo):
    ...
    package myDemo;
    In another , I do following :
    ...
    import myDemo.*;
    (seems more like a maledictus domini at the moment)
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    Packaging is basically used in Java to make class names unique, and to impose a certain order among your .class files. It's analogous to namespaces. For example, your application may be using two different Date classes so a naming conflict is possible.
    Let's define two sample packages. Suppose my classpath root is at C:\anthony\java\applications. I create a folder named "drink" and inside I have:

    I create another folder C:\anthony\java\applications\language and inside I have:

    Note the package declarations at the top of each. Finally I need a client class at C:\anthony\java\applications:

    Note the import declarations at the top. Also, I am using the fully qualified names of both Java classes to disambiguate them. I could have used
    import language.*;
    import drink.*;
    which means "import all classes inside the language and drink packages". Finally, the classes I defined the package had access modifiers like public and private, which specifies if they are accessible outside the package, accessible between classes with an inheritance relationship, accessible between classes inside the package, or being inaccessible to any other class at all.
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    Okay, let's do some tweaking. Let's change the class drink.Java a bit:

    I added the toString() method here. This method is overriding the toString() method of class Object, which is a superclass of drink.Java. I didn't have to do anything -- all classes have class Object as a superclass. Finally I change the client class as well:
    achana chan
    Ranch Hand

    Joined: Jul 29, 2002
    Posts: 277
    Hi !
    Thanks .
    achana chan
    Ranch Hand

    Joined: Jul 29, 2002
    Posts: 277
    Hi !
    One more question on this thread. I noticed that you have classes within classes, socalled subclasses. This is vcommon in the stack of JAVA books infornt on me. However I still have problems (in the olden days, I would say it is "scoping" and "visibility" ):

    Javac has this complain :
    AllEmpDS.java:71: cannot resolve symbol
    symbol : variable out
    location: class connections.AllEmpDS
    out.println(sb.toString());
    and yet, I have declare the variable out in the public method doGet().
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    out is a local variable of doGet() so it only has scope inside doGet(). Referring to out inside showAllEmp() causes this error. Why not use public boolean showAllEmp(PrintWriter out) instead?
    achana chan
    Ranch Hand

    Joined: Jul 29, 2002
    Posts: 277
    Hi !
    Once again, thanks.
    I was doing things like
    public boolean showAllEmp(out)
    etc
    Do you work for JavaRanch ?
    Anthony Villanueva
    Ranch Hand

    Joined: Mar 22, 2002
    Posts: 1055
    Originally posted by achana chan:
    Do you work for JavaRanch ?

    We're all volunteers here.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Behaviour of return() ???