class

Static inner class example

This is an example of how to use a static inner class. We have set the example as described below:

  • We have created a class, ArAlgo that contains a static inner class P.
  • Class P has two double attributes and their getters.
  • ArAlgo class also has a static method, P min_max(double[] vals). The method gets a double array and for each value in the array and computes the minimum
    and the maximum value in the array.
  • We create a new double array and fill it with random values, using random() API method of Math. Then we get a new instance of P class, using min_max(double[] vals) method of ArAlgo class, with the double array created above. We use the getters of the ArAlgo class to get the values of the two fields.

Let’s take a look at the code snippet that follows:  

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.javacodegeeks.snippets.core;
 
public class StaticInnerClass {
 
    public static void main(String[] args) {
 
 
  double[] num = new double[20];
 
  for (int i = 0; i < num.length; i++) {
 
 
num[i] = 100 * Math.random();
 
  }
 
  ArAlgo.P p = ArAlgo.min_max(num);
 
  System.out.println("min = " + p.getF());
 
  System.out.println("max = " + p.getS());
    }
}
 
class ArAlgo {
 
    /**
     * A pair of floating-point numbers
     */
    public static class P {
 
 
  /**
 
   * Constructs a pair from two floating-point numbers
 
   */
 
   
 
  private double f;
 
   
 
  private double s;
 
 
  public P(double a, double b) {
 
 
f = a;
 
 
s = b;
 
  }
 
 
  /**
 
   * Returns the first number of the pair
 
   */
 
  public double getF() {
 
 
return f;
 
  }
 
 
  /**
 
   * Returns the second number of the pair
 
   */
 
  public double getS() {
 
 
return s;
 
  }
    }
 
    /**
     * Computes both the minimum and the maximum of an array
     */
    public static P min_max(double[] vals) {
 
   
 
  double min = Double.MAX_VALUE;
 
  double max = Double.MIN_VALUE;
 
   
 
  for (double v : vals) {
 
 
 
 
 
if (min > v) {
 
 
    min = v;
 
 
}
 
 
if (max < v) {
 
 
    max = v;
 
 
}
 
  }
 
  return new P(min, max);
    }
}

Output:

min = 1.5117631236976625
max = 90.86550459529965

  
This was an example of static inner class in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button