📄 prog.html
字号:
<CODE>static println</CODE> method in the <CODE>System</CODE> class, without creating an instance of the <CODE>System</CODE> class.<P>However, a program must create an instance of a class to access its non-staticfields and methods. Accessing static and non-static fields and methods isdiscussed further with several examples in the next section. <A NAME="fields"></A><H3>Fields and Methods</H3>The <A HREF="./Code/LessonTwoA.java">LessonTwoA.java</A> program alters the simple example to store the text string in a static field called <CODE>text</CODE>. The <CODE>text</CODE> fieldis static so its data can be accessed directly without creating an <EM>instance</EM> of the <CODE>LessonTwoA</CODE> class. </FONT><PRE>class LessonTwoA { static String text = "I'm a Simple Program"; public static void main(String[] args){ System.out.println(text); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <A HREF="./Code/LessonTwoB.java">LessonTwoB.java</A> and<A HREF="./Code/LessonTwoC.java">LessonTwoC.java</A> programsadd a <CODE>getText</CODE> method to the program to retrieve and print the text. <P>The <A HREF="./Code/LessonTwoB.java">LessonTwoB.java</A> programaccesses the non-static <CODE>text</CODE> field with the non-static<CODE>getText</CODE> method. Non-static methods and fields are called instance methods and fields. This approach requires that an instance of the <CODE>LessonTwoB</CODE> class be created in the <CODE>main</CODE> method. To keep things interesting, this example includes a static text field and a non-static instance method (<CODE>getStaticText</CODE>) to retrieve it.<BLOCKQUOTE><HR><STRONG>Note:</STRONG> The field and method return values are all type <CODE>String</CODE>.<HR></BLOCKQUOTE></FONT><PRE>class LessonTwoB { String text = "I'm a Simple Program"; static String text2 = "I'm static text"; String getText(){ return text; } String getStaticText(){ return text2; } public static void main(String[] args){ LessonTwoB progInstance = new LessonTwoB(); String retrievedText = progInstance.getText(); String retrievedStaticText = progInstance.getStaticText(); System.out.println(retrievedText); System.out.println(retrievedStaticText); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The <A HREF="./Code/LessonTwoC.java">LessonTwoC.java</A> programaccesses the static <CODE>text</CODE> field with the static <CODE>getText</CODE> method. Static methods and fields are called class methods and fields. This approach allows the program tocall the static <CODE>getText</CODE> method directly without creatingan instance of the <CODE>LessonTwoC</CODE> class. </FONT><PRE>class LessonTwoC { static String text = "I'm a Simple Program";//Accessor method static String getText(){ return text; } public static void main(String[] args){ String retrievedText = getText(); System.out.println(retrievedText); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">So, class methods can operate only on class fields, and instancemethods can operate on class and instance fields. <P>You might wonder what the difference means. In short, there is onlyone copy of the data stored or set in a class field but each instance hasits own copy of the data stored or set in an instance field. <P><IMG SRC="./Art/diff.gif" WIDTH="436" HEIGHT="158" ALT=""><P>The figure above shows three class instances with one static field and one instancefield. At runtime, there is one copy of the value for static Field A andeach instance points to the one copy. When setFieldA(50) is called on thefirst instance, the value of the one copy changes from 36 to 50 and allthree instances point to the new value. But, when setFieldB(25) is calledon the first instance, the value for Field B changes from 0 to 25for the first instance only because each instance has its own copy ofField B.<P>See<A HREF="http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html">Understanding Instance and Class Members</A> lesson in <A HREF="http://java.sun.com/docs/books/tutorial/index.html">The Java tutorial</A> for a thorough discussion of this topic. <A NAME="const"></A><H3>Constructors</H3>Classes have a special method called a <EM>constructor</EM> that is called when a class instance is created. The class constructor always has thesame name as the class and no return type.The <A HREF="./Code/LessonTwoD.java">LessonTwoD</A> program converts the <CODE>LessonTwoB</CODE> program to use a constructor to initialize the text string. <BLOCKQUOTE><HR><STRONG>Note:</STRONG> If you do not write your own constructor, the compiler adds an empty constructor, which calls the no-arguments constructor of its parent class. The empty constructoris called the default constructor. The default constructorinitializes all non-initialized fields and variables to zero.<HR></BLOCKQUOTE></FONT><PRE>class LessonTwoD { String text;//Constructor LessonTwoD(){ text = "I'm a Simple Program"; }//Accessor method String getText(){ return text; } public static void main(String[] args){ LessonTwoD progInst = new LessonTwoD(); String retrievedText = progInst.getText(); System.out.println(retrievedText); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="summ"></A><H3>To Summarize</H3>A simple program that prints a short text string to the consolewould probably do everything in the <CODE>main</CODE> methodand do away with the constructor, <CODE>text</CODE> field, and<CODE>getText</CODE> method. But, this lesson used a very simpleprogram to show you the structure and elements in a basicJava program.<A NAME="more"></A><H3>More Information</H3>See<A HREF="http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html">Understanding Instance and Class Members</A> lesson in<A HREF="http://java.sun.com/docs/books/tutorial/index.html">The Javatutorial</A> for a thorough discussion of this topic.<P>_______<BR><A NAME="TJVM"><SUP>1</SUP></A> As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> <CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-2000 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -