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
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
}
}
Like I have such code:
class myobj {
public static int i;
public myobj(int _i) {
i = _i;
}
}
public class mainclass {
public static void main(String[] args) {
myobj a = new myobj(1);
myobj b = new myobj(2);
System.out.println(b.i); // Prints 2 - expected
System.out.println(a.i); // Prints 2 - unexpected
}
}
And I want a.i to be 1.
How can I make a 'new' object?
Remove the static declaration. Declaring something as static means that it will be shared across all instances of a class. So in your code, both a and b were using the same i variable. If we just remove the static modifier, your code works as expected.
class myobj {
public int i;
public myobj(int _i) {
i = _i;
}
}
public class mainclass {
public static void main(String[] args) {
myobj a = new myobj(1);
myobj b = new myobj(2);
System.out.println(b.i); // Prints 2 - expected
System.out.println(a.i); // Prints 2 - unexpected
}
}
Make this:
public static int i;
this:
public int i;
Everything else is fine.
Given what you've asked change this
public static int i;
to
public int i;
Because a static field is shared by all instances of myobj.
if you write your class that way
public static int i;
only one i will be created and this i will be shared with all object of myobj ever created.That what static key word means and used for. Read this thread for better understanding. I if want i once a myobj created remove the static keyword
public int i;
public class Name{
int b = 100;
public void get(){
System.out.println(b);
}
public int num(){
return b;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(b);
}
}
get method can access b, num method can access b as well. why not public static void main method can access b.
Because b is an instance field, scoped within instances of the Name class.
Your main method is static, scoped within the class itself.
Declare b as static to be able to access it from the main method.
You'll also need to make methods get() and num() static to compile.
It's an instance (non-static) field, so you need an instance to reference it:
public static void main(String[] args) {
System.out.println(new Name().b);
}
Static methods can only access static properties. You could either make b static, or you can instead instantionalize Name:
public class Name{
int b = 100;
public void get(){
System.out.println(b);
}
public int num(){
return b;
}
public static void main(String[] args) {
new Name(args);
}
public Name(String[] args) {
System.out.println(b);
}
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.
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;
}
}