Why should my method be static? [duplicate] - java

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
Hi I have this piece of code and i am really confused to why i have to make the lel method static.The error is this "non static method cant be referred from static content". Usually when I create methods either to construct new objects or to manipulate objects in the main method I do not get this error message.Plus, i never declared e to be static!!. can someone please explain to me why this occurring?? Thank you :)
class x {
public static void main(String[]args){
int e= 2232;
e= lel(e);
}
int lel(int k){
return k+1;
}
}

There are two solutions you could implement. The first option is to make your int lel(int k) a static method which would look like static int lel(int k)
Your other option is to declare a new object of your class x and use that for your lel method within main as MickMnemonic suggested in the comments. That code would look like:
e = new x().lel(e);
I believe the simplest thing would be to make the lel method static but it is up to you.
A deeper explanation of static methods can be found here.

Related

Can you define methods in the main 'public static void main(String[] args)' method in Java? [duplicate]

This question already has an answer here:
Methods inside methods [duplicate]
(1 answer)
Closed 10 months ago.
I'm new to Java and have been practising programming within the Eclipse IDE. I have the following code:
public static void main(String[] args) {
public double absoluteValue(double n) {
if (n < 0) {
return -n;
} else {
return n;
}
}
System.out.println(absoluteValue(-5));
}
I tried to create a method absoluteValue() which returns absolute values. However, Eclipse threw a bunch of errors at me.
I managed to get my code to work when I defined absoluteValue() outside of the main method (had to define it as public static double absoluteValue(double n) though) and called it within the main method.
But I was just wondering why the above code doesn't work. Can you define methods within the main method (if so, should you)?
Java does not allow nesting of methods of this sort. You can have lambdas inside of a method but you can't define a full method inside of a method. This is an important difference for Pythonistas to know.

"Which variable(instance or local) is in action in the public method of foo class?" [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Is "this" shadowing a good idea?
(10 answers)
Closed 3 years ago.
It seems like its using local variable of the method!!!! I thought it would give an error on call non static variable on static method but it did not.
public class foo{
int x = 12;
public static void go(final int x){
System.out.println(x);
}
}
Actually it doesn't have a error which is interesting..
System.out.println(x)
will print the parameter of the method go
If you want to access field of the class, use this keyworld (if your method not static):
System.out.println(this.x)
In your case, you need to have an instance of foo class and use
foo f = new foo();
System.out.println(f.x);

static interface variables getting inherited ......... why? [duplicate]

This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
I am learning interfaces in java and the source from which i am learning clearly says that static variables do not get inherited. But for some reason, I am able to access that static variable without adding the interface name before it. I want to know why is this happening and an in depth explanation of whats going on !!!? plzz help
class StaticMethods {
public static void main(String [] com) {
TourClient t = new TourClient(); // i made this a class variable in place of interface variable just for demonstration
t.check();
}
}
interface Tour {
///This stuff is just for display, doesn't play a role
static float minimalCost = 50000;
static float maximumCost = 1000000;
static float recommendedRating = 3.9f;
static int minimumVisitingPlaces = 4;
}
interface DubaiTour extends Tour {
static float Rating = 4.4f;
}
class TourClient implements DubaiTour{
void check() {
System.out.println(Rating); // This is not giving me any errors!!
}
}
NOTE :- I found a stack overflow page Does static variable inherited?
, but this does not explain in depth why is this happening, which doesn't help me
Static variables are inherited.
Once again - static variables are inherited - BUT you shouldn't use them. The reason why is, if you build your program. For optimization, your TourClient class variables get substituted with the constants. The line System.out.println(Rating) gets replaced with System.out.println(4.4) - all is well. If you edit your interface and change a variable to, say, 5.5, it won't get updated in your TourClient class. It will probably still print 4.4. When you use static variables in an interface, you need to recompile EVERYTHING. Not just the files you change.

Instance variable value not being printed [duplicate]

This question already has answers here:
non static variable name cannot be referenced from a static context [duplicate]
(5 answers)
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 6 years ago.
I am trying to learn java. currently learning about types of variables.
i have written a small program defining instance,local,static variables and trying to print the same from with in the main method. but i am getting error saying "non static variable i cannot be referenced from static context. Below is my program
public class variable{
int i=5;
static int j=10;
public static void main(String[] args){
int k=15;
System.out.println(i);
System.out.println(j);
System.out.println(k);
}
}
Please let me know whats wrong with the program
You need to create a instance of variable and access i
variable v = new variable();
// then access v.i
BTW use Camelcase for you class name.
int i should be static becasue static context cant refer to the non-static variable
Options:
Make a new instance of your class so you can reach i. In fact it is maybe not the best option, because you should make it private, and add a getter method... :)
OR
You could change int i to static int i, because of the static main method.
+1 : it is better to have classnames camescased... :)

How do I access an int array in another class in java? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
This is a simple code that focuses on the problem so I can apply this to the actual program.
The first class has the array. The second I want it to print array elements to the scanner. This code gives me a null pointer exception. I tried using an int instead of int array but still got the same exception. How can I fix this?
public class passthrough{
public void main(String[] args){
PassTry k = new PassTry();
System.out.println(k.pass);
System.out.println(k.her);
}
}//class
public class PassTry{
public int[] pass;
public int her;
public PassTry(){
her = 2;
pass = new int[]{4, 5, 6, 2};
}//constructor
public int res(){
return this.her;
}//res method
}//class
You are missing static Keyword in main method so you need to write like this,
public static void main(String args[]) {...
Each word has different meaning and different purpose,
Static : Keyword which identifies the class related this. It means that this class is not instance related but class related. It can be accessed without creating the instance of Class.
Java compiler always look for main method to compile the source code but here is you didn't provide static so compiler is unable to reach main method and trowing compile time error which is java.lang.NullPointerException

Categories