I would just like a clear example of how to instantiate a public final class in Java. I have to use a method from a class like this for a project, and have no idea how to instantiate it in the first place. Very difficult to find a clear example and explanation of the proper syntax. Thanks for the help.
Asked
Active
Viewed 4,330 times
-1
-
See http://stackoverflow.com/questions/5181578/use-of-final-class-in-java – user998692 Sep 07 '13 at 06:45
-
What makes you think there is anything special about final? I mean, if you just tried you would have realized it works normally... – Janick Bernet Sep 07 '13 at 06:58
-
http://stackoverflow.com/help/asking – Aniket Kulkarni Sep 07 '13 at 07:25
4 Answers
0
public class Test {
public static void main(String[] args) {
Project pro = new Project();
pro.getName();
}
}
final class Project{
public String getName(){return "";}
}
===============================
A final class can be created like a normal class, Only thing is it can not be extended

upog
- 4,965
- 8
- 42
- 81
-
Hmmm...I am not sure why this regular call is not working for me. I don't have an issue creating a final class, that is pretty much all that people have been able to show me how to do. I have an issue calling that class. For some reason my instructors have made the constructor for this final class private, so I can't just: "Project pro = new Project();" other than doing this I have no idea how to call a class. – NooberBLucky Sep 07 '13 at 07:07
-
In the constructor is private, then you will have a public static method like getInstance(), which would return the reference of that Class. you can use that method to get the Object Reference. Probably the class will be singleton. If you can, then share the code for reference try Project p = project.getInstance(); // the method name may vary as per your implementation – upog Sep 07 '13 at 07:12
-
Ok that sounds like something helpful upog...can you show me an example of using a method to reference a final class. I have tried a number of syntax but keep getting errors in my error check. the method I need is called public static int uniform(int N) and the class is StdRandom. So I try this: StdRandom.uniform(N) and it is wrong StdRandom s = new StdRandom(s.uniform(N)); "just desperate here" int a = static StdRandom.uniform(N); – NooberBLucky Sep 07 '13 at 07:24
-
A class with private constructor can never be instantiated outside the class – upog Sep 07 '13 at 07:24
-
So I cannot use the methods within that class? Then I must be looking at the wrong class then. What is the use of such a class? – NooberBLucky Sep 07 '13 at 07:27
-
Singleton class has lots of usage,you can get reference as i mentioned in my next post. With that ref you can call non static methods – upog Sep 07 '13 at 07:36
0
This is an example
public class del {
public static void main(String args[])
{
x x1=new x();
System.out.println(x1.u());
}
}
final class x
{
public String u()
{
return "hi";
}
}
As you can see,x is a final class and have a method u which returns a string. I am instatiating x in class del and calling its method u. The output is hi
For more info click on final

SpringLearner
- 13,738
- 20
- 78
- 116
0
final class Test{
public void callMe(){
System.out.println("In callMe method.");
}
}
public class TestingFinalClass{
public static void main(String[] args){
Test t1 = new Test();
t1.callMe();
}
}
Output : In callMe method.
final
in java is applied to variable,method,class
- final variable : the variable can not be signed with another value.
- final method : the method cannot not be overridden.
- final class : the class cannot extended.
The best example is String class in java. public final class String
you can access the methods of String class as normal.
Some links

Aniket Kulkarni
- 12,825
- 9
- 67
- 90
0
public class Test {
public static void main(String[] args) {
StdRandom stdRandom = StdRandom.getInstance(); /* this will retun an instance of the class, if needed you can use it */
int result =StdRandom.uniform(1);
System.out.println(result);
}
}
final class StdRandom{
private static StdRandom stdRandom = new StdRandom();
private StdRandom(){
}
public static StdRandom getInstance(){
return stdRandom;
}
public static int uniform(int N){
// Implement your logic here
return N;
}
}

upog
- 4,965
- 8
- 42
- 81
-
upog there is no getInstance() method in the class, and I can't change anything in the file. There is also no private static Instantiation of stdRandom within the final class either. Is this the only way to utilize a method from this class. – NooberBLucky Sep 07 '13 at 07:37
-
It is not mandatory to have a method that will return the Object reference. If the method you want to use is static then try ClassName.staticMethodName() it should work, if not post your code for reference, to comment more on that – upog Sep 07 '13 at 07:43
-
Thanks upog that seems to work....I don't understand why, but hopefully I will soon. So for this final class I do not need to instantiate it, and can just call methods this way. Good to know. – NooberBLucky Sep 08 '13 at 01:04