473,443 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Final Keyword In Java

BarryA
19 16bit
Using a final keyword in the main method parameter solves the problem. Why is there no compiler error or exception since I modified Java's standard main method?

Expand|Select|Wrap|Line Numbers
  1. public class bytes {
  2.  
  3.     public static void main(final String... args) {
  4.         System.out.println("Hi");
  5.  
  6.     }
  7. }
Now let's see if I can code:

Expand|Select|Wrap|Line Numbers
  1. public class bytes {
  2.  
  3.     public static void main (String... args) {
  4.  
  5.         String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
  6.         args[0] = "hi";
  7.         System.out.println(args[0]);
  8.         args =str;
  9.         for(int i=0; i<args.length; i++) {
  10.             System.out.print(args[i]);
  11.         }
  12.  
  13.     }
  14.  
  15. }
When you execute the programme using the above coding, pass an argument as:

Expand|Select|Wrap|Line Numbers
  1. javac bytes Nisrin
The output of my programme is

Expand|Select|Wrap|Line Numbers
  1. hi
I have yet to receive a response.
Now repeat the process with the final keyword.

Expand|Select|Wrap|Line Numbers
  1. public class bytes {
  2.  
  3.     public static void main (final String... args) {
  4.  
  5.         String[] str = {"I ", "haven't ", "received ", "my ", "answer." };
  6.         args[0] = "hi";
  7.         System.out.println(args[0]);
  8.         args =str;
  9.         for(int i=0; i<args.length; i++) {
  10.             System.out.print(args[i]);
  11.         }
  12.  
  13.     }
  14.  
  15. }
It throws an error saying that the final parameter, args, cannot be assigned.

Because I am now assigning str to args.

As I am following this source https://www.scaler.com/topics/java/f...yword-in-java/, this indicates I have made a significant difference by include the final keyword in the argument and making it constant in the main method. I'm updating the primary method's signature. So, why am I not receiving any compilation or runtime errors?
Mar 31 '23 #1
0 407

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Anthony Martin | last post by:
I've been reading the Java Language Specification, and in Chapter 16 there's an interesting topic called Definite Assignment. http://tinyurl.com/3fqk8 I'm wondering about the idea of "Deferred...
2
by: G. Ralph Kuntz, MD | last post by:
Step on my soapbox... :-) I believe that the "final" keyword is underused in Java. I have gotten into the habit of using it on EVERY method parameter, instance variable, and local variable...
14
by: Medi Montaseri | last post by:
Hi, I think my problem is indeed "how to implement something like java's final in C++" The long version.... I have an abstract base class which is inherited by several concrete classes. I...
14
by: My4thPersonality | last post by:
Has the fact that both Java and C# are garbage collected, and C++ in not, anything to do with the fact that there is no language item to prevent a class from being inherired from? I once read that...
1
by: silverburgh.meryl | last post by:
I am trying to convert this code from java to c++: public final class Type { public static final int DEFAULT = 1; private static int index = 2; public static final int COLUMN1 = (int)...
1
sanjay123456
by: sanjay123456 | last post by:
dear friends , is there r in c++ available final keyword so we prevent derviation of a class sanjay
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
5
by: kiran | last post by:
Hi all, How can we stop a base class to be derived in C++? In java we do that using final keyword. Is there a c++ equivalent? Thanks in advance, Kiran
4
by: puneetsardana88 | last post by:
class abc1 { public void abc2(abc my) { this=my; } } class abc3 { public static void main(String args)
1
BarryA
by: BarryA | last post by:
I was curious why the class java.lang.String is defined as final in Java when I first learned about it. I couldn't find an answer back then, but check out this post: How to Create a Java String Class...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.