Java Basic knowledge Keyword __java

Source: Internet
Author: User

Java Basic Knowledge Keywords

Definition: Some words that have been given a specified meaning

Root class Related: Public private protected class interface implements extends import package abstract

Root method function/attribute Related: Void return static final

Eight basic data types: int short byte long char boolean double float

Circular judgment: For switch does while break continue if case

Exception: Try catch throw throws finally

Other: This super

Goto (reserved word)

First, to analyze the permission modifiers:

Establish the following structure for experiment

1.public: Free access to the entire project, whether across classes or across packages

Package Text1;
public class People {public
	String name;
	public void Eat () {}
}

Package text2;
Import Text1. People;
public class Mans {public
	static void Main (string[] args) {
		people people = New people ();
		People.name = "";
		People.eat ();
	}

The public property name and method eat are defined in the package Text1 and can be accessed in the package Text2, which can be seen as a very large access permission.

2.protected: Classes under the same package can be accessed arbitrarily, not across packages (cross-package inheritance is a protected property or method that can be inherited)


Package Text1;
public class People {public
	String name;
	protected String sex;
	public void Eat () {}
	protected void Drink () {}
}

Define protected properties sex and methods in people drink

Package text2;
Import Text1. People;
public class Mans {public
	static void Main (string[] args) {
		people people = New people ();
		People.name = "";
		People.eat ();
		people.<del>sex</del> = "";
		People.<del>drink</del> ();
	}
}

You can see that you can't call it in man, there's a way to access the properties and methods that are protected decorated.

Package text2;
Import Text1. People;
public class Woman extends people{public
	static void Main (string[] args) {
		Woman people = new Woman ();
		People.name = "";
		People.eat ();
		People.sex= "";
		People.drink ();
	}
Defining a subclass woman inherits the parent class people, you can invoke the protected property and method of the parent class with the subclass instance.

3. Default does not write: the same package of classes can be accessed at will, not cross the package


Package Text1;
public class People {
	int age;
	void Play () {}
}
<pre class= "java" name= "code" >package Text2;
Import Text1. People;
public class Woman extends people{public
	static void Main (string[] args) {
		Woman people = new Woman ();
		People.<del>age</del> =;
		People.<del>play</del> ();
	}

Satchel inheritance You also cannot access the properties and methods of the default decoration

Package Text1;
public class Student {public
	static void Main (string[] args) {
		people people = New people ();
		People.age =;
		People.play ();
	}

Other classes under the same package can access the default properties and methods

4.private: Only the current class can access it itself, other classes have objects and cannot access private properties/methods


Package Text1;

public class People {


	private String address;

	private void Sleep () {} public
	static void Main (string[] args) {
		people people = New people ();
		People.address = "";
		People.sleep ();
	}

Package Text1;
public class Student {public

	static void Main (string[] args) {
		people people = New people ();
		people.<del>address</del> = "";
		People.<del>sleep</del> ();
	}

Private property and method errors are invoked with other classes of the package.

Summarize a table:

Access modifiers: Public protected default private four types of access rights

Current class Current Package Sub Class External Package
Public Ok Ok Ok Ok
Protected Ok Ok Ok
Default Ok Ok
Private Ok

The other four modifiers are decorated in the range: Methods, properties, classes

The class can only be decorated with public and default write-only.

Second, the keyword naming

Swith () {//int/short/string (JDK1.7 version not previously available) enum type

Case **:break;

}

Iii. the difference between the This\super

This: object for the current class

Super: Object of the parent class

Normal method and property calls: this. Method name (), this. Property name

This ();

Super. Method Name (), Super. Property

Super ();

Note: 1.this () and super () must appear in the first row

2. Method overrides, if you want to supplement the method of the parent class, you need to call the super. Parent class Method Name () method definition

Iv. final (Ultimate, Final)

Can be used to decorate: classes, methods, properties, parameters, local variables

Cosmetic class: Current class cannot be inherited

Method: The current method cannot be overridden

Attribute: can only be assigned once (constant) on behalf of the current property

Parameter/local variable: cannot be assignable on behalf of the current parameter within the lifecycle scope

V. Static (stationary)

Can be used to decorate: methods, properties, static blocks

Note: Static methods must not use Non-static properties or methods directly










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.