
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is a forward reference in JShell in Java 9?
JShell is a command-line tool that allows us to enter Java statements (simple statements, compound statements, or even full methods and classes), evaluates it and prints the result.
Forward references are commands that refer to methods, variables, or classes that don't exist in any code we have typed in JShell. As code entered and evaluated sequentially in JShell, these forward references have temporarily unresolved. JShell supports forward references in method bodies, return types, parameter types, variable types, and within a class.
In the below code snippet, created a method forwardReference() in Jshell. This method can't be invoked until the variable is declared. If we are trying to attempt to call this method, it throws a warning message: "attempted to call method forwardReference() which cannot be invoked until variable notYetDeclared is declared"
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> void forwardReference() { ...> System.out.println(notYetDeclared); ...> } | created method forwardReference(), however, it cannot be invoked until variable notYetDeclared is declared jshell> forwardReference() | attempted to call method forwardReference() which cannot be invoked until variable notYetDeclared is declared
In the below code snippet, we have declared the "notYetDeclared" variable that returns a string. Finally, if we call forwardReference() in JShell, then it prints "variable is declared".
jshell> String notYetDeclared = "variable is declared" notYetDeclared ==> "variable is declared" jshell> forwardReference() variable is declared
- Related Articles
- How to declare reference types in JShell in Java 9?
- JShell in Java 9?
- What are the useful commands in JShell in Java 9?
- What is the use of the Tab key in JShell in Java 9?
- How to debug JShell in Java 9?
- Package Imports in JShell of Java 9
- Using Variables in JShell of Java 9
- What are the different shortcut keys in JShell in Java 9?
- What are the different feedback modes in JShell in Java 9?
- What are the different "/edit" commands in JShell in Java 9?
- What are the different startup scripts in JShell in Java 9?
- What are the different "/types" commands in JShell in Java 9?
- What are the different "/vars" commands in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create a thread in JShell in Java 9?
