Home Java javaTutorial Understanding of objects and references and inner classes in object-oriented programming in Java

Understanding of objects and references and inner classes in object-oriented programming in Java

Jan 20, 2017 pm 05:36 PM

I recently watched think in java when I was off work, and it feels very different from the first time I watched it again
Next, let’s talk about the relationship between objects and references in Java, and the concept of internal classes.
1. Everything in java is an object
What is the object that operates in java? The answer is a reference, which is like a pointer in C or C++.
If you have a reference, you must associate it with an object at this time, otherwise the reference will not be under your control as you imagine. For example, if you create a String reference:

String s ;
Copy after login

It is not associated with any object at this time. If you do some operations at this time, such as calling some methods of String, problems will definitely occur (except for some basic types, Because they will be assigned initial values ​​when you define them), the money must be associated with the object when using it:

String s = new String();
Copy after login

or

String s = “my name is ajun”;
Copy after login

Just like this.
2. How to associate with an object
In java, an object is usually created through new to associate with a reference, such as:

String s = new String("my name is ajun")
Copy after login

This not only creates An object is associated with a reference s and initialized simultaneously. At the same time, we can also create our own object type.
3. Storage location
(1) Stack: Generally stores references and basic type variables. The stack mainly allocates and releases memory by moving the stack pointer up and down.
Basic type variables are not suitable for creation with new because they occupy a small amount of memory.
(2) Heap: used to store java objects. When the program executes new, the heap will allocate a space to the object. Remember that the allocation and release of memory by the heap is more expensive than the storage and release of memory by the stack. This means that basic type variables need to be stored on the stack, because basic type variables are used most frequently, and memory is stored and released frequently. When more is consumed, the performance can be imagined.
4. Internal classes
(1) Basic knowledge of internal classes:
Generally, classes defined inside the java class become internal classes
Internal classes can be divided into: classes defined outside the method body, Define classes inside the method, static inner classes (can only be defined outside the method), anonymous inner classes
Note:
Classes defined outside the method:
Member variables of the class (static, non-static) can Access, in order to ensure that the member variables of the class can be correctly referenced, the object of the outer class must be instantiated first before the object of the inner class can be instantiated.
Access permissions can be any, and can be regarded as member variables of the class. This makes it much easier to understand.
Class defined in the method body;
The member variables (static and non-static) of the class can be accessed. In order to ensure that the member variables of the class can be correctly referenced, the object of the external class must be instantiated first. You cannot have access permission to the object of the instantiated inner class, just treat it as a local variable of the method.
Static inner class:
Only static member variables of the class can be accessed
Access permissions Any
Anonymous inner class:
Member variables (static, non-static) of the class can be accessed, in order to ensure that Correctly reference the member variables of the class, so the object of the external class must be instantiated first before the object of the internal class can be instantiated
Access permissions cannot be
(2), the role of the internal class
Internal Classes can hide classes very well. Generally, classes are not allowed to have private protect default access rights.
Inner classes can achieve multiple inheritance, which makes up for the fact that java cannot have multiple inheritance
(3), example

package com.ajun.test.innerclass.example;
  
/**
 * 水果内容
 * @author Administrator
 *
 */
public interface Contents {
   String value();
}
 
package com.ajun.test.innerclass.example;
  
/**
 * 水果目的地
 * @author Administrator
 *
 */
public interface Destination {
  
  //目的地
  String readLabel();
}
 
package com.ajun.test.innerclass.example;
  
public class Goods {
  
  private String des="is ruit!!";
    
  //方法外部
  private class Content implements Contents{
    private String name = "apple "+des;
    @Override
    public String value() {
      return name;
    }
  }
    
  //方法外部
  private class GDestination implements Destination{
    private String label ;
    private GDestination(String label){
      this.label= label;
    }
    @Override
    public String readLabel() {
      return label;
    }
  }
    
    
  //匿名内部类
  public Destination getdestination(final String label){
    return new Destination(){
      @Override
      public String readLabel() {
        return label;
      }
    };
  }
    
  public Destination dest(String s){
    return new GDestination(s);
  }
    
  public Contents content(){
    return new Content();
  }
    
  public Destination dest2(String s){
    class GDestination implements Destination{
        private String label;
        private GDestination(String label){
          this.label= label;
        }
        @Override
        public String readLabel() {
          return label;
        }
    }
    return new GDestination(s);
  }
    
}
 
package com.ajun.test.innerclass.example;
  
public class Test {
  
  public static void main(String [] a){
    Goods gs = new Goods();
    Contents c = gs.content();
    Destination d = gs.dest("Beijing");
    System.out.println(c.value());
    System.out.println(d.readLabel());
    Destination d1 = gs.getdestination("Shanghai");
    System.out.println(d1.readLabel());
    System.out.println(gs.dest2("Tianjin").readLabel());
  }
}
Copy after login

Among them, Content and Gdestination are obtained It is well hidden. When calling from the outside, you don't know which specific class is being called, making this class have the feature of multiple inheritance.

apple is ruit!! 
Beijing 
Shanghai 
Tianjin
Copy after login
For more articles related to the understanding of objects and references and internal classes in Java’s object-oriented programming, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles