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()
Related
I would like to know if it is possible from a child class to get caller attributes. Follow a sample.
TestClass1.class
package test;
public class TestClass1 {
private String myTestClass1Var = "xxxxx";
public static void main(String[] args) {
new TestClass1().class2();
}
private void class2() {
new TestClass2().getClass1Attributes();
}
}
TestClass2.class
package test;
public class TestClass2 {
public void getClass1Attributes() {
//is something like this possible?
getCaller().myTestClass1Var
}
}
Thanks
A class end up exposing all its methods and fields under the same level and get extremely unorganized.
Would it be better to organize it in specialized sub-classes. Below is an example of a messy class.
public class MyClass
{
//[Properties]
public String value1;
public String value2;
//[Methods used by outside world]
public void job1()
{
job1_task1();
job1_task2();
utilInternal1();
utilExternal1();
}
public void job2()
{
job2_task1();
utilInternal1();
utilExternal1();
}
//[Methods used locally and always specific to a job method]
private static void job1_task1() ...
private static void job1_task2() ...
private static void job2_task1() ...
//[Methods used locally and general to jobs methods]
private static void utilInternal1() ...
//[Methods that could be added to an external class util]
public static void utilExternal1() ...
}
So far i have organized my code in sub specialized classes such as Props, Jobs, Tasks, Utils.Internal, Utils.Externals.
Instance class like Jobs and Props. Are exposed via two instance fields. They will use directly instance variable.
Static class can be directly used such as Tasks and Utils. They will NOT use directly instance variable.
public class MyClass
{
public Prop Props = new Prop();
public Job Jobs = new Job();
//[Properties]
private class Prop
{
public String value1;
public String value2;
}
//[Methods used by outside world]
public class Job
{
public void job1()
{
Tasks.Job1.task1();
Tasks.Job1.task2();
Utils.internal.util1();
Utils.external.util1();
}
public void job2()
{
Tasks.Job2.task1();
Utils.internal.util1();
Utils.external.util1();
}
}
//[Tasks for Jobs]
private static class Tasks
{
//[Tasks for job1]
private static class ForJob1
{
private static void task1() ...
private static void task2() ...
}
//[Tasks for job2]
private static class ForJob2
{
private static void task1() ...
}
}
private static class Utils
{
//[methods util used locally]
private static class Internal
{
private static void util1() ...
}
//[methods util generic that could be added to an external class Util]
private static class External
{
public static void util1() ...
}
}
}
I made static most of the sub class containing methods that must not use directly class variable, instead they will ask them as parameters.
Tasks are useless to external class and only have meaning locally in our class.
private static Task
{
private static ForJob1
{
private static String task1(String value)
{
String result = value + ".";
return result;
}
}
}
Is it something that is acceptable ?
My concern is also about the memory usage ?
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 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;
}
}