Thinking in Java 4 Learning (ii) final,protected keywords

Source: Internet
Author: User
Tags export class rand

Protected keywords:

This is private for class users, but it is accessible to any export class that inherits from this class or any other class that is within the same package .

Final keyword:

(1) Final data

1. Final data is either initialized at the time of definition or initialized in each constructor, otherwise compiled.

2. The final modifier base type keeps the values constant; The final decorated object reference keeps the reference constant, but the object itself can be modified.

(2) Final method

There are two reasons for using the final method (a) to lock the method and prevent any inherited classes from modifying it (ii) for efficiency. But now it's just the first reason to consider

Final vs Private

All private methods in a class are implicitly specified as final (because the private method cannot be overridden and cannot be overwritten)

(3) Final class

The final class cannot be inherited

 Public class Value {    int  i;      Public Value (int  i) {        this. i = i;    }} 

ImportJava.util.Random; Public classFinalData {Private StaticRandom Rand =NewRandom (47); PrivateString ID;  PublicFinalData (String id) { This. ID =ID; }    Private Final intValueone = 9; Private Static Final intValue_two = 99;  Public Static Final intValue_three = 39; Private Final intI4 = Rand.nextint (20); Static Final intInt_5 = Rand.nextint (20); PrivateValue V1 =NewValue (11); Private FinalValue v2 =NewValue (22); Private Static FinalValue Val_3 =NewValue (33); Private Final int[] A = {1, 2, 3, 4, 5, 6 };  PublicString toString () {returnID + ":" + "I4 =" + I4 + ". Int_5 = "+int_5; }     Public Static voidMain (string[] args) {FinalData fd1=NewFinalData ("Fd1"); //!fd1.valueone++;//error:the final field Finaldata.valueone cannot//Be assignedfd1.v2.i++; Fd1.v1=NewValue (9);  for(inti = 0; i < fd1.a.length; i++) {Fd1.a[i]++; }        //!fd1.v2 = new Value (0);//error:the final field Finaldata.valueone//cannot be assigned//!fd1. Val_3 = new Value (1);//error:the final field Finaldata.val_3//cannot be assigned-the static field finaldata.val_3 should be//accessed in static//!fd1.a = new Int[3];////error:the final field Finaldata.valueone//cannot be assignedSystem.out.println (FD1); System.out.println ("Creating New FinalData"); FinalData FD2=NewFinalData ("Fd2");        System.out.println (FD1);    System.out.println (FD2); }}

Input Result:

FD1:I4 = 15. Int_5 =new= 15. Int_5 =13. Int_5 = 18

Here's a question : I4 and int_5 are random numbers, so why does the main method result be the same no matter how many times it runs?

See Chapter Seventh: Reusable Classes

Thinking in Java 4 Learning (ii) final,protected keywords

Related Article

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.