Question:
1. pls what are the differences btw static and non-static fields?
Maryam B
2009-12-01 01:23:11 UTC
2.What’s the difference between arrays of primitive types vs. arrays of reference types?
Three answers:
anonymous
2009-12-01 01:34:58 UTC
Well the difference between static and non-static fields is that non-static fields are instance members (which means they have a unique incarnation for each instance of the class you make) whereas static fields are class members, which means that they're always the same for all instances of a class. Static does NOT mean that it stays the same and never changes like mick_rons says - that's the final keyword. For example...



public class Class {

public int non_static_field = 0;

public static int static_field = 0;



public static void main(String[] args) {

Class c1 = new Class();

Class c2 = new Class();



// this changes the non-static field in each instance of Class

c1.non_static_field = 1;

c2.non_static_field = 2;



// this changes the static-field, which is reference via the class of Class, rather than the instance

Class.static_field = 1;



}



As for the second part of your question... Reference types, for example String, are similair to using pointers in C. An array of primitive types, for example int, is an array of fixed size in memory where each location in the array will store an instance of this primitive type. An array of reference types will store a reference do a different location in memory, rather than the actual object itself, and that location denotes where the memory is actually stored.



Hope this helps
PalmTrees
2009-12-01 02:32:06 UTC
Hi,

A static variable:

variable that has been allocated statically (lifetime ) across the entire run of the program, it remains (its value) in memory until you end your program.

Non static variable:

variable that removed from the memory when you exit from procedure or close window, or end your program.

Reference types arrays:

Arrays can be passed as arguments to method parameters. Because arrays are reference types, the method can change the value of the elements.

http://msdn.microsoft.com/en-us/library/hyfeyz71.aspx

http://msdn.microsoft.com/en-us/magazine/cc301755.aspx
mick_rons
2009-12-01 01:32:57 UTC
static means it stays the same and never changes


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...