Results 1 to 4 of 4

Thread: clone().?

  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default clone().?

    package first;

    class G extends Thread
    {
    public void run()
    {
    System.out.println("ur in run method of thread");
    }

    public static void main(String[] args)
    {
    G g = new G();
    System.out.println(g.getClass());
    System.out.println(g.getState());
    System.out.println(g.getPriority());
    System.out.println(g.getId());
    System.out.println(g.isDaemon());
    System.out.println(g.getContextClassLoader());
    g.start();
    try
    {
    System.out.println(g.clone());
    System.out.println(g.getState());
    System.out.println(g.getPriority());
    System.out.println(g.getId());
    System.out.println(g.isDaemon());
    System.out.println(g.getContextClassLoader());

    }
    catch (Exception e) {
    // TODO: handle exception
    }
    }
    }


    when i run the above program i got the below output,why the statements in try block are never executed

    class first.G
    NEW
    5
    9
    false
    sun.misc.Launcher$AppClassLoader@5acac268
    ur in run method of thread
    -----------------------------------------------------------------------------------------
    -----------------------------------------------------------------------------------------
    BUT when i commented the statement System.out.println(g.clone()); i got the below output
    class first.G
    NEW
    5
    9
    false
    sun.misc.Launcher$AppClassLoader@5acac268
    RUNNABLE
    5
    9
    false
    ur in run method of thread
    sun.misc.Launcher$AppClassLoader@5acac268


    has i am beginner in java,can any one tell me what actually the clone() method do System.out.println(g.clone());


    Thanks in advance
    Daya
    Last edited by Dayanand; 02-11-2011 at 01:24 PM.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    10

    Default

    )) It is nice.
    Java Code:
    } catch (Exception e) {
    // TODO: handle exception
    }
    So How you detect that try/catch block not invoke?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    11

    Wink

    Quote Originally Posted by Dayanand View Post
    why the statements in try block are never executed
    Those statements do execute.

    How about putting a Stack Trace inside the catch block? The whole purpose of catch block is to check what went wrong and you are not doing it.

    Try this,

    Java Code:
    catch (Exception e) {
          e.printStackTrace();
          }
    and see the magic... :eek:

    After seeing that, you might need to have a look at Cloneable interface and clone method details.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

    Default

    Clone only does something useful if the class you are calling it on implements it in a useful way. For example, one might want to clone some form of array - which would mean you would want to get an array just like the original, but a copy. This becomes difficult when objects contain other object types - how do you clone them?

    Is there a specific thing you are trying to use clone() for?

Similar Threads

  1. How to clone a JTable?
    By IamKira in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 05:28 PM
  2. How to clone an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  3. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 09:47 AM
  4. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:46 AM
  5. clone problem
    By feniger in forum New To Java
    Replies: 13
    Last Post: 01-13-2008, 10:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •