• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Loading

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)Hi while loading and initialising...can someone explain the order
of class variables and constructors. Bit confused here.
2)What does it mean by an interface can not define instance fields, methods must all be instance methods?
3) Can an interface extend a class?
- Thanks

[This message has been edited by Doit (edited August 21, 2000).]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !!
1) I think this is the order for intialization for a class in general.
*First all the class variables(static var) and static initializers are executed in the textual order ie order in which they appear in the class. Here if the class extends ie has a superclass etc., then the same is done for the supermost class first, then for each subclasses in order.
But note that static means - not related to each instance, so when the first obj of a class is created all the static initialization for that class is over, and that shall be used by any no of instances created later. So this step is done for the first instance of a class only.
* As per the JVM specf next, the constructor invocation begins and the instance variables are initialized and the constructor code then executed , again the superclasses to subclasses in order . This will be for each instance of that class.
You can refer to this url under initialization for more info: http://www.javasoft.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19075
2) The variables in the interfaces are static and final. So naturally they cannot be associated with each class implementing that interface, but act like constants being the same for all classes implementing that interface.
But with methods, all of them are abstract and hence cannot be static. they are invoked with their obj instance only.
3) Interfaces can extend other interfaces only.
If any of the info is incorrect, hope someone will let me know.
Regards
Usha
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi doit,
for the first question the loading of the constructors and variables start form the super class and passes subsequently to its subclasses
for the third question an interface cannot extend a class but it can extend an interface only
please correct me if i am wrong
regds
thejus/mahesh
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Usha
u wrote :-
"But note that static means - not related to each instance, so when the first obj of a class is created all the static initialization for that class is over, and that shall be used by any no of instances created later. So this step is done for the first instance of a class only."
Let me clarify one thing :
Static initialization is independent of creating the object.
"when the first obj of a class is created all the static initialization for that class is over" ( see ur text)
To be more clear please run this code
public class NoObject
{
static
{
System.out.println("Look i am being executed without Any Object");
}
public static void main(String args[])
{}
}
Static initialization takes place at the time of loading the class that is when u write c:\> java NoObject
the NoObject.class file is loaded into the memory. JVM first execute the static part of the most super class in the downward direction till static initialization over for all sub classes.
member variables,code segments and constructors are executed ony when u create the object of the class.

Please correct me if i am wrong

[This message has been edited by Satyen Sharma (edited August 22, 2000).]
 
Usha Kasi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satyen,
I realise that my first point was not wrong but incomplete, so thanks to u for ur followup. As per the JVM, static initialization occurs when one of these happen:
1) An instance of a class is created
2) A static method in a class is invoked
3) A static nonconstant field is used or assigned.
For reference see the topic Initialization in : http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#19075
So,in the code that u have given, since the static method main is invoked, static initialization takes place and the line is printed, eventhough no instance of that class was created.
Hope I have been correct and complete now !!
Regards
Usha
 
Satyen Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Usha
Thanx for making the things more clear to me.


[This message has been edited by Satyen Sharma (edited August 23, 2000).]
 
I didn't say it. I'm just telling you what this tiny ad said.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic