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;
Related
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 am new to Java and am trying to access method variables outside of the method, but it doesn't work.
Code is below:
public class MethodAccess {
public static void minus() {
int a=10;
int b=15;
}
public static void main(String[] args) {
//Here i want to access variable names a & b that is in minus()
int c = b - a;
}
}
Because a and b are local variables.
If you want to access to them in your main method, you need to modify your code. For example :
public class methodacess {
private static int a;
private static int b;
public static void minus(){
methodacess obj =new methodacess();
a=10;
b=15;
}
public static void main (String[] args){
int c = b - a;
}
}
The variables that are defined inside a method are local to that method, so you cannot use them outside. If you want to use them outside, define instance variables in the beginning of your class.
I think you might want to do it hte other way around:
public class methodacess {
public int minus(int a, int b){
int c = b - a;
return c;
}
public static void main (String[] args){
// Here youi want to call minus(10, 15)
int a=10;
int b=15;
System.out.println("Result is: " + minus(a, b))
}
}
You need to define the variables as static class variables, so you can access them from a static function. Also watch out for the access modifiers, since when the variable is private you can't access them outside any other class.
public class methodacess {
private static int a;
private static int b;
public static void minus(){
methodacess obj =new methodacess();
a=10;
b=15;
}
public static void main (String[] args){
//Here i want to access variable names a & b that is in minus()
int c = b - a;
}
}
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'm new in programming and I would like to know where did I go wrong in instantiating an object. Below is the code:
public class Testing{
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
Sample myTest = new Sample();
System.out.println(c);
}
}
There is no Sample class in your code . The one which you have declared is a private method .
// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
With the current snippet , You need to instantiate the Testing class and make use of the Sample method. Notice your class definition is preceded by the keyword class , in this case class Testing.
public class Testing{
private int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
Testing t = new Testing(); // instantiate a Testing class object
int result = t.Sample(1); // use the instance t to invoke a method on it
System.out.println(result);
}
}
But that doesn't really make sense, your Sample method always returns 3 .
Are you trying to do something like this :
class Sample {
int a;
int b;
Sample(int a, int b) {
this.a = a;
this.b = b;
}
public int sum() {
return a + b;
}
}
public class Testing {
public static void main(String[] args) {
Sample myTest = new Sample(1, 2);
int sum = myTest.sum();
System.out.println(sum);
}
}
I doubt you actually want to create an object.
From your code snippet, I understand that you want to run a 'method' named Sample which adds two numbers. And in JAVA you don't have to instantiate methods. Objects are instances of class. A method is just a behavior which this class has.
For your requirement, you don't need to explicitly instantiate anything as when you run the compiled code JAVA automatically creates an instance of your class and looks for main() method in it to execute.
Probably you want to just do following:
public class Testing{
private int sample(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int c = sample(1, 2);
System.out.println(c);
}
}
Note: I changed Sample to sample as it's generally accepted practice to start a method name with lower-case and class name with an upper-case letter, so Testing is correct on that front.
You instantiating correctly with new keyword ,But your calss name and method invoking is wrong
Testing myTest = new Testing();
int result =myTest.Sample(1); //pass any integer value
System.out.println(result );
Sample is not a class, it is just a method. You cannot create instances of it.
You only run it -
int sample = Sample(3);
If you wish for sample to be a class, define it as a class.
In your case, the method is not static is so you cannot directly access it from the Static method Main. Make it static so you could access it. Or just create a new instance of Testing and use it -
Testing testing = new Testing();
int sample = testing.Sample(3);
Sample method returns integer, so get the result and use it anywhere.
public static void main(String []args)
{
int myTest = Sample(4555);//input may be any int else
System.out.println(myTest);
}
This is how you should be doing this.
public class Testing{
public int Sample(int c)
{
int a = 1;
int b = 2;
c = a + b;
return c;
}
public static void main(String []args)
{
// Creating an Instance of Testing Class
Testing myTest = new Testing();
int c =0;
// Invoking the Sample() function of the Testing Class
System.out.println(myTest.Sample(c));
}
Well , it's easy. To you instantiate an object in Java you should to use class name and to do with that it class received a valor. For exemplo :
...Car c1 = new Car();
If I make for example one project. Inside with two class. For example: X and Y. I make them what I want, and I want to make a main method in Y. Only system.out.printlf the values in X and Y. But it writes that I need to make them static if I want to run this. I tried to make a new file with only the main class and inside the X Y values but it showed an error. What I missed?
you have missed object creation. Try X x = new X(); in your Y file. I would recommend to read some tutorials on Java, starting from here.
The main method is declared static
public static void main(String[] args) {}
Inside of main, it can only access static variables that exist in the enclosing class. You will see this for example with this bit of code:
public class X {
private int i = 5;
public static void main(String[] args) {
System.out.println(i);
}
}
To make the above work you need to declare i as static:
public class X {
private static int i = 5;
public static void main(String[] args) {
System.out.println(i);
}
}
A better way would be to do this:
public class X {
private int i = 5;
public X() {
System.out.println(i);
}
public static void main(String[] args) {
new X();
}
}
Static methods can only access static methods and other variables declared as static.
This article may also help you to understand what is going on here.
I'm guessing that's because everything happens inside the main method which is indeed static right? e.g.
public class C {
int X;
int Y; //or whatever other type
..
public static void main(String args[]) {
System.out.print( X ); //this won't work!
}
}
instead use this aprroach:
public class C {
int X;
int Y; //or whatever other type
..
public static void main(String args[]) {
C inst = new C();
System.out.print( c.X ); //this will work!
}
}
The main method is static and can only access static fields from the class. non static fields belong to an instance/object which you'd have to create:
public class X {
static int a = 0;
int b = 0;
public static void main(String[] args) {
System.out.println(a); // OK -> accesses static field
System.out.println(b); // compile error -> accesses instance field
X x = new X();
System.out.println(x.b); // OK -> accesses b on instance of X
}
}