Solved

Java Questions

Posted on 2000-05-14
4
193 Views
Last Modified: 2010-04-01

1)if an object is created inside the static block of a class when it will be destroyed?
will it be destroyed when then are no more objects of class containing the static block?
please Explain.

2)How weak reference is used to determine when an object is no longer being referenced?

3)Does the List class Supports doubly linked list only or single linked list is also provided by it?

Thanks sridhar
0
Comment
Question by:kesavan_sridhar
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
Comment Utility
1) If an object is created inside the static block of a class when it will be destroyed? Will it be destroyed when then are no more objects of class containing the static block? Please Explain.

Answer: The static initialization block exists to allow "complex" initialization of static class members. According to the JVM spec, a class will be initialized immediately before an instance of the class is created, a static method is invoked, or a nonconstant static field is referenced or assigned. As with initialization, the lifetime of static members is directly related to the lifetime of the class itself. As long as the class is loaded, its static members will be accessible. The rules for class unloading, as of Java 2, are that a class may only be unloaded if its class loader is unreachable. The bootstrap class loader is always reachable; as a result, system classes may never be unloaded. In practice, this means that once a class is loaded, it is unlikely that it will ever be unloaded. This prevents problems that were prevalent in pre-jdk1.2 days with classes implementing the Singleton pattern being unloaded improperly. In short, assuming you don't release the object at some other time, an object created in a static initialization block will be effectively available for the lifetime of your application.

----------
2) How weak reference is used to determine when an object is no longer being referenced?

Answer: The traditional definition of unreachable object which is a candidate for garbage collection candidate an object to which there are no more active references. Reachable objects consist of those pointed to by class variables (both static and instance), objects pointed to by local variables in the currently active methods of all threads, and any other objects reachable from these referenced objects. This is the traditional definition. Weak, Soft, and Phantom references change the picture by introducing new kinds of references that permit more refined collaboration with the garbage collector. Weak references are a way of keeping a reference to an object but considering this reference as "second-class". When no standard references to the object exist, the garbage collector is welcome to reclaim it even if there are weak references. So all the old garbage collection rules still apply. A weak reference is just a way of saying, "I need a reference to this object because I'm doing something for another object (my client) that depends on this. When my client doesn't need this reference anymore, then neither do I. Furthermore, I don't want my client to have to tell me when that happens."

----------
3) Does the List class Supports doubly linked list only or single linked list is also provided by it?

Answer: List is an interface that defines the operations to be supported by an ordered collection (or sequence). There are several concrete implementations of List including the LinkedList class which IS a doubly-linked list. Its iterator, returned by LinkedList.listIterator is a ListIterator which extends Iterator to allow traversal of the list in either direction and modification of the list during traversal.

Best regards,
Jim Cakalic
0
 
LVL 7

Expert Comment

by:Ravindra76
Comment Utility
Hi JIM:-

      For some reasons JOD ( one of Top 15 java experts )in EE is not participatig in
      this site from some time.

      You are filling that vacuum with Your excellent comments

      Both of Ur style is same in answering questions.

      I will appreciate it.

      Thanks

:)
0
 

Author Comment

by:kesavan_sridhar
Comment Utility
Thank You Very much for Your answers,JIM

Bye sridhar
0
 

Author Comment

by:kesavan_sridhar
Comment Utility
Thanks

Bye sridhar
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now