This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java: how to call non static method from main method?
I am finding this a bit hard to implement.
I have a String[] called name which is declared globally.
String[]name;
Now i want to access this variable from the main() method. The main method is static therefore how could i access it.
I tried Animal.name but it didn't work.
How can i do this?
You need to create an instance of Animal class to access instance fields: -
Animal animal = new Animal();
animal.name; // Access array
You can solve this with two different ways, each requiring code modification:
First is to create an object of Animal type and accessing the name property.
Second is to make name as static.
like this: static String[] name = new String[10];
Related
This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 4 years ago.
My getter wants the method it's getting to have a static method but if I do that it will break my code is there any way around this?. Language Java.
This is the line in my test class that wants to get the method in my vending class.
System.out.println("Total Sales for both machines today $"+ VendingMachine.getTotalsSales());
This is my getter method that takes the array I created from the string provided in the test class that put the items into an array. I want to be able to keep this array that I have created while also getting the total sales.
public double getTotalsSales() {
totalSales += availableItems[this.selectItem].price;
return totalSales;
}
Is there any way I can keep my array and with also being able to grab the total sales from the tester class?
It sounds like VendingMachine is the name of your class, not the name of an instance of your class. Calling the method getTotalsSales on the class instead of an instance is trying to call it in a static context, which would be why it asks for it to be static.
Create an instance of the class and call it on that instead!
I assume that VendingMachine is the class name. You need to first create an instance of this class:
VendingMachine machine = new VendingMachine();
Then call the method on that instance:
machine.getTotalsSales()
For more details, I suggest you read a tutorial on the difference between classes and objects and how they are related.
This question already has answers here:
Difference between Static methods and Instance methods
(10 answers)
Closed 4 years ago.
Whenever I have to call a method from another class, I first create an object and then call it through the object. But while I was writing some code, I mistakenly wrote classname.methodname(); and it worked.
I would usually write,
classname obj = new classname();
obj.methodname();
Here is the actual code:
Class 1
public class Dataset {
public static List<ECCardData> getDataset() {
//Code
}
in Class 2
List<ECCardData> dataset = Dataset.getDataset();
I noticed that the methodname() was static. Was that the reason?
Yes, for static methods (with suitable access modifier) you can call directly with your class by
YourClass.yourMethod();
and this way, too
YourClass anObject = new YourClass();
anObject.yourMethod();
Happy coding.
I hate answering my question, but I found the correct answer.
When a method is declare static, only one instance of that method will exist. When you create an object a new instance of the method is created, which is not possible for a static method. Therefore you use the class name.
classname.methodname(); //only one instance
classname obj;
obj.methodname(); //instance with obj as Object(IDE gives warning, should not be allowed, ideally)
The basic paradigm in Java is that you write classes, and that those
classes are instantiated. Instantiated objects (an instance of a
class) have attributes associated with them (member variables) that
affect their behavior; when the instance has its method executed it
will refer to these variables.
However, all objects of a particular type might have behavior that is
not dependent at all on member variables; these methods are best made
static. By being static, no instance of the class is required to run
the method.
You can do this to execute a static method:
classname.staticMethod();//Simply refers to the class's static code But
> to execute a non-static method, you must do this:
>
> classname obj = new classname();//Create an instance
> obj.nonstaticMethod();//Refer to the instance's class's code
This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
New to Java, trying to figure out how to resolve this issue.
boolean myBool = G(A,n,m,0);
For some reason it isn't like this line. Why won't it let me call this simple function? Both main() and G() are part of class C().
A non static method belongs to a specific instance of a class, while a static method belongs to the class itself. Inside main, which is a static method, you cannot reference non-static methods without having a specific object to run them. E.g.:
boolean myBool = new C().G(A,n,m,0);
However, if the class has no interesting state, or it's state does not effect the method G, you should define G as static.
It's likely because you didn't include static in the definition of the G() method.
Main() is a static method, and since static things run before non static things do, static things can only call/use static things.
Note that your Main() doesn't require you to make a C object. It's the entry point to the program, and it doesn't make sense if you have to first make an object in order to run your program - where would you make that object from?
If you want to make non-static calls, create objects of the corresponding class.
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I found an instantiation wich I do not understand how works.
The instantiation looks like this:
public static Class instance[] = new Class[arraySize];
If my guessing is right, the instance is an array?
How will this work?
This declares an array of Class objects references. It is equivalent to the other syntax of [] after the type.
You would access it just like a normal array:
instance[0] = ...
instance[1] = ...
public static Class instance[] = new Class[arraySize];
public is the access modifier. This one means that this variable is visible in your entire project
static means that this variable is a "class" field it means that it belongs to the entire class and you can access it by ClassName.nameOfTheVariable or if you are accessing it from inside of class it is declared you could use just nameOfTheVariable.
Class in this context is a type and you should treat is as a type of object
[] means that this is an array you could also write Class[]
= is assignment operator
new is the word that annunciates that there will be memory allocation and constructor invokation after it
After new there is initialization of array of arraySize length.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Which one is the better way of code writing?
Hi everyone,
I would like to know if there is any difference between two initialization methods below. If so, which method is considered as a best practice? Thanks in advance.
Class Foo {
List myList = new ArrayList();
}
Class Foo {
List myList;
public Foo() {
myList = new ArrayList();
}
}
if you know what you are going to initialize a class member to, it is better to do it as the first example. In that case, you are creating a new ArrayList. You should also make it private final unless you intend to change it through class methods.
I only initialize things in the constructor if the constructor takes arguments that are applied to members - otherwise I do it outside of it.
I think it's a matter of style... I prefer the former when possible, but the latter when necessary.
Personally I prefer the second (creating new objects in constructor). But there's no difference between them.
The only difference maybe if you have two objects created by both methods, object which is created by first method is created first and object which is created in constructor would be created after it.