JAVA 6 (object)

Source: Internet
Author: User

Static keyword:


Usage: Used to modify members (member variables, member functions)   feature 1 is loaded as the class loads and disappears as the class disappears. Indicates the longest life cycle. 2 takes precedence over the existence of an object, static exists first, and the object is present. 3 shared by all objects 4 can be called directly by the class name    persion.country;  Use Note: 1, static methods can only access static members (because static exists first), non-static and can access non-static member 2, Static can not be defined in the This,super3, the main function of static profit: The object's shared data to be stored in a separate space, save space, there is no need for all objects to be stored in a copy, directly called by the class name.   Disadvantages: The life cycle is too long, access is limited.   Unique content as objects are stored in heap memory, common content is stored in the shared area with static adornments. Static content is shared by the object, and only a single content is created in memory (stack), which saves memory.    class Zx {  public static void Main (String args[])   {    persion p = new Persio N ();   p.name = "Zhangsan";   p.show (); }} class persion { string name;  static String country = "CN";  public void Show ()  {  System.out.println (name+ "::" +country);  } }  using Scenario 1, when shared data is present in the object, it is statically decorated, the data is defined as non-static, in the heap memory 2, when the function does not have access to non-static data, the function can be defined as static      Class Zx {  public static void Main (String args[])   {    persion p = new Persion ();   & nbsp  p.show (); }} class persion {&nbsp String Name; static String country = "CN";  public static void Show ()  {  System.out.println ("haha");  } }  Although you can use Arraytool to build methods for objects using these tools, there are problems: 1, objects are used to encapsulate data, arraytool objects do not encapsulate unique data 2, Each method of manipulating an array does not use the unique data in the Arraytool object   Consider making the program more rigorous, do not need the object, you can define the methods in the Arraytool as static, directly call the method with the class name   example:  public Class Arraytool{ public int Getmax (int [] arr)  {  int max=0;  for (int x=0;x<arr.length;x++)    if (Arr[x]>max)     max=arr[x];  return max; } public int getmain (int [] arr) & nbsp {  int min=arr[0];  for (int x=0;x<arr.length;x++)    if (arr[x]< min)     MIN=ARR[X];   return  min; } public void Selectsort (int [] arr)  {  for (int x=0;x<arr.length-1;x++ )    for (int y=x+1;y<arr.length;y++)     if (Arr[x]>arr[y])     {      Swap (arr,x,y);   } }  public void bubblesort (int []arr) &NBSP;{&NBSp for (int x=0;x<arr.length-1;x++)    for (int y=0;y<arr.length-x-1;y++)     if (arr[y]>arr[y +1])     {     swap (arr,y,y+1);   } }  public void swap (int []arr,int A,int b)  {  int temp=arr[a];  arr[a]=arr[b];  arr[b]=temp; }}   class Zx{  public static void Main (String []arr)  {  int[] p = {1,9,99,66,2,33,6,88,44,66,78,26};  Arraytool Tool = new Arraytool ();  int max = Tool.getmax (P);  System.out.println ("Max" +max);  Tool.selectsort (p); & nbsp for (int x=0;x<p.length;x++)   {   system.out.println (p[x]); }   Tool.bubbleSort (p) ;  for (int x=0;x<p.length;x++)   {   system.out.println (p[x]); }  }}      Modified:  public class arraytool{ public static int Getmax (int [] arr)  {  int max=0;& nbsp for (int x=0;x<arr.length;x++)    if (Arr[x]>max)     max=arr[x];  return max; } public static int getmain (int [] arr)  {  int mi n=arr[0];  for (int x=0;x<arr.length;x++)    if (arr[x]< min)     min=arr[x];  return  min; } public static void Selectsort (int [] arr)  {  for (int x=0;x<arr.length-1;x++)    for (int y=x+1;y<arr.length;y++)     if (Arr[x]>arr[y])     {      Swap (arr,x,y);   } }  public static void Bubblesort (int []arr)  {  for (int x=0;x <arr.length-1;x++)    for (int y=0;y<arr.length-x-1;y++)     if (arr[y]>arr[y+1])     {     swap (arr,y,y+1);   } }  public static void swap (int []arr,int A, int b)  {  int temp=arr[a];  arr[a]=arr[b];  arr[b]=temp; }}  class Zx{  public static void Main (String []arr)  {  int[] p = {1,9,99,66,2,33,6,88,44,66,78,26}; //arraytool tool = new Arraytool ();  int max = Arraytool.getmax (p);  System.out.println ("Max" +max);  Arraytool.selectsort (p);  for (int x=0;x<p.length;x++)   {   system.out.println (P[x]); }   arraytool.bubblesort (p);  for (int x=0;x<p.length;x++)   {   system.out.println (p[x]); }  }}   When the method is static, it is easy to use, but the class can also be used by other programs to establish objects , in order to be more rigorous, so that the class cannot establish objects, you can provide the privatization of the constructor to complete. This way, you can no longer create objects. Because the constructor is initialized when the object is initialized, the constructor cannot be initialized to create a new object.  public class Arraytool{ private Arraytool () {} public static int getmax (int [] arr)  {  int max= 0;  for (int x=0;x<arr.length;x++)    if (Arr[x]>max)     max=arr[x];  return max;  } public static int Getmain (int [] arr)  {  int min=arr[0];  for (int x=0;x<arr.length;x++) & nbsp  if (arr[x]< min)     min=arr[x];  return  min; } public static void Selectsort (int [] arr)  {  for (int x=0;x<arr.length-1;x++)    for (int y=x+1;y<arr.length;y++)     if (Arr[x]>arr[y])     {     swap (arr,x,y);   } }  public static void Bubblesort (int []arr)  {  for (int x=0;x<arr.length-1;x++)    for (int y=0;y< arr.length-x-1;y++)     if (arr[y]>arr[y+1])     {     swap (arr,y,y+1);   } }  private public static void swap (int []arr,int a,int b)  //Hide  {  int temp=arr[a] ;  arr[a]=arr[b];  arr[b]=temp; }}   Static code block: Loaded as the class is loaded and executed only once. Used to initialize a class. Constructs a block of code that takes precedence over an object's initialization, takes precedence over the constructor to initialize the corresponding object, and takes precedence over the main function.   Print: A,c,d The following is the new Staticcode (5); Format: Static{system.out,println ("a")}     Singleton design mode:

JAVA 6 (object)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.