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.
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
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
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
final keyword
String class
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;
}
}
Related
The boolean has to be outside the main method so other methods can manipulate it. I've searched everywhere and cannot find a suitable answer because all I stumble upon are solutions for booleans as methods. It has to be a simple boolean and it cannot be static. Don't have much time, so any help would be great. Thanks.
public class myClass {
private int[][] holdsStuff;
private boolean isNeeded;
public setFalse (){
}
public setTrue () {
}
public static void main(String[] args) {
//call methods to change isNeeded
//require isNeeded to prevent invalid changes being made to holdsStuff
}
}
If class member isNeeded is not static, then it must belong to an instance of myclass, you can create a new instance and manipulate this instance:
public class myClass {
private int[][] holdsStuff;
private boolean isNeeded;
public void setFalse (){
isNeeded = false;
}
public void setTrue () {
isNeeded = true;
}
public static void main(String[] args) {
myClass mc = new myClass();
myClass.setFalse();
}
}
You want to use isNeeded in other methods and in main method which is static and static methods just deal with static data read this. so what you want to do is make instance of this class to call isNeeded in main method
public static void main(String[] args) {
myClass myclass = new myClass();
boolean isNeeded = myclass.isNeeded;
}
public class myClass {
private int[][] holdsStuff;
private boolean isNeeded;
public void setFalse (){
isNeeded =false;
}
public void setTrue () {
isNeeded = true;
}
public static void main(String[] args) {
myClass myclass = new myClass();
myclass.setFalse();
myclass.setTrue();
//call methods to change isNeeded
//require isNeeded to prevent invalid changes being made to holdsStuff
}
}
since main is static either use static keyward or make an instance of the same class. use static key the following:
public class myClass {
private int[][] holdsStuff; // make this static if you are also this inside main
private static boolean isNeeded;
public static setFalse (){
}
public static setTrue () {
}
public static void main(String[] args) {
//call methods to change isNeeded
//require isNeeded to prevent invalid changes being made to holdsStuff
}
}
How can I call getDummy from main? I need this so I can pass dummy to a method in another class.
public class Test {
public static void main(String[] args) {
private int dummy = 0;
}
public int getDummy() {
return dummy;
}
}
getDummy is an instance method so you need the instance
public static void main(String[] args) {
Test t = new Test();
t.getDummy();
}
and this belongs to the class
private int dummy = 0;
your final code could look like>
public class Test {
private int dummy = 0;
public static void main(String[] args) {
Test t = new Test();
t.getDummy();
}
public int getDummy() {
return dummy;
}
}
Is this what you mean?
public class Test {
private int dummy = 0;
public static void main(String[] args) {
Test test = new Test();
int dummy = test.getDummy();
}
public int getDummy() {
return dummy;
}
}
I assume private int dummy = 0; is a property (variable) of Test class. Calling a non-static method from a static method is not allowed. You create an instance of your class in the static method and can call any of its public methods.
you should be declare target object, and initialization, then you can use getDummy(), or you can modify getDummy() method to static .
sry, my english very bad, but i think i can help you. first, you create a public variable out of main, in you code, you only create a local variable. next, in your main, you type : "getDummy();". good luck
I have a class which has static final variables. I want to intialize it at boot up time, not when they are first referenced. Can I have an empty static init() method in the class which will serve the purpose?
Example
public class ABC {
private static final SomeObject abc = new SomeObject();
//other methods and variables
public static void init(){
//empty method
}
}
public class DEF{
public class static void main(String[] args) {
ABC.init();
}
}
You can use a static block and initialize it there. Your init() method might end up initializing them every time you create an object.
public class ABC {
private static final SomeObject abc = new SomeObject();
//other methods and variables
static{
//initialization code.
}
}
public class DEF{
public class static void main(String[] args) {
ABC a =new ABC();
}
}
guys i9 know it is dummy qustion but i am beginner .. i have this class
public class threadLocal {
private static ThreadLocal<String> myThreadLocal;
public threadLocal(){
myThreadLocal = new ThreadLocal<String>();
}
public static ThreadLocal<String> getMyThreadLocal() {
return myThreadLocal;
}
public static void setMyThreadLocal(ThreadLocal<String> myThreadLocal) {
threadLocal.myThreadLocal = myThreadLocal;
}
}
and i want to call it like this in another class
myThreadLocal.setMyThreadLocal("patrick");
so what changes should i do in threadLocal class ??
I think you should rewrite your class this way.
public class threadLocal {
private static ThreadLocal<String> myThreadLocal = new ThreadLocal<String>();
public static String getMyThreadLocal() {
return myThreadLocal.get();
}
public static void setMyThreadLocal(String str) {
myThreadLocal.set(str);
}
}
While calling, you may just call
threadLocal.getMyThreadLocal()
I have 2 classes the static main class and class B. I'm trying to pass main to B, where there is a method that sets fields.
Can this be done?
If so, could you please provide examples?
public static void main(String[] args) {
ArrayList a = new ArrayList()
class b = new class()
b.update(b);
}
class a {
public void update(ArrayList a) {
//updates the encapsulated arrayList field
}
}
The error message keeps on saying that one is static and the other is non-static, but they should be pointing the same object
I'm not entirely sure what you are trying to do, but here is an example that shows that you can pass an instance of the main class into another class:
public class A {
private String str = null;
public static void main(String[] args) {
A a = new A();
B b = new B(a);
System.out.println(a.getStr());
}
public String getStr() {
return this.str;
}
public void setStr(String str) {
this.str = str;
}
}
public class B {
public B(A a) {
a.setA("hello");
}
}
Running this code will print out hello.
main is static and public, so you can call it from any other class as any other public static method: statically.
if you have a class A that contains a
public static void main(String[] args)
method, then class B can call this method like
A.main(s);
where s is String[]
your question is far from clear, so I suggest you to add more code samples to make it clear what you're really trying to do.