Error when declaring & initializing string as static in java [duplicate] - java

This question already has answers here:
How do I declare a static variable inside the Main method?
(6 answers)
Closed 6 years ago.
I'm trying to declare and initialize local String variable as static , but there is a compilation error showing Illegal modifier static. Why is it so?
Here is my code:
public class StringInstance {
public static void main(String[] args) {
static String s = "a";
if(s instanceof String){
System.out.println("Yes it is");
}
}
}

The String you are declaring will be static because it's scope is static, so you don't need the static modifier. But if you want to declare it static outside the scope of main() then do it like this:
public class StringInstance {
static String s = "a";
public static void main(String[] args) {
if(s instanceof String){
System.out.println("Yes it is");
}
}
}

Related

Does static variable inherited? [duplicate]

This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.
class s
{
static int x;
}
class aaa extends s
{
void fun()
{
x=2;
System.out.println(x);
}
public static void main(String args[])
{
aaa w=new aaa();
w.fun();
}
}
static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.

can public static final string variables be overridien in sub-classes? [duplicate]

This question already has answers here:
How to inherit static field and change it's value?
(4 answers)
Closed 7 years ago.
Here is the sample program:
public class Base {
public static final String FOO = "foo";
public static void main(String[] args) {
Base b = new Base();
Sub s = new Sub();
System.out.print(Base.FOO);
System.out.print(Sub.FOO);
System.out.print(b.FOO);
System.out.print(s.FOO);
System.out.print(((Base)s).FOO);
}
}
class Sub extends Base {
public static final String FOO="bar";
}
When I execute this it's printing foobarfoobarfoo.
Since String FOO is declared as public static final, my understanding is its value cannot be changed anymore. But in the subclass Sub, the value is being changed to bar.
Shouldn't the program throw a compilation error?
Why is it printing foobarfoobarfoo?
Static variables are not inherited, they belong to the class, that's why they are static. The subclasses can have static fields with the same, though.

error when calling a void function [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Cannot make a static reference to the non-static method
(8 answers)
Closed 8 years ago.
public class Makakiesmarkou {
void swap(int i, int j, int[] arr) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public void MySort(int[] T)
{
for(int m=0; m<T.length-1; m++)
{
int j=m;
for(int k=m+1; m<T.length-1; k++)
{
if(T[k]<T[j])
j=k;
}
swap(T[j], T[m], T);
}
}
public static void main(String[] args) {
int[] pin= new int[50];
MySort(pin);
System.out.println(Arrays.toString(pin));
}
}
the error when i call MySort in the main class is "non static method MySort[int[]] cannot be referenced from a static context"
what am i doing wrong?
You can either do what Salah said, or you can instantiate your class and call MySort on that:
public static void main(String[] args) {
int[] pin= new int[50];
Makakiesmarkou m = new Makakiesmarkou();
m.MySort(pin);
System.out.println(Arrays.toString(pin));
}
You need to change the declaration of your method to be static like:
public static void MySort(int[] T)
static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class.newInstance().
So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which make sense to disallow non static or instance variable inside static context is non existence of instance.
Read more here

Use of Static in the example [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 8 years ago.
I want to know the meaning of static in my file. Everytime, I come across an error and then find out that the word static is missed. Can you please explain when and where should the word static be used.
Here, for example in the code, even if I remove static from the variable, it shows error.
Is, it static class, so it has to be static variable ??
public class TestJavaServer {
static String xCordinate;
public static void main(String[] args)
{
readFromFile();
}
public static void readFromFile() throws IOException
{
xCordinate ="something";
}
A static field is the same in all instances of the class.
Do
TestJavaServer.xCordinate ="something";
and Bob's your uncle.
However, rewrite your code like this instead:
public class TestJavaServer
{
String xCordinate;
public static void main(String[] args)
{
TestJavaServer testJavaServer = new TestJavaServer();
testJavaServer.readFromFile();
}
public void readFromFile()
{
xCordinate ="something";
}
}

Why am I receiving a "non static variable" error when calling this Java class? [duplicate]

This question already has answers here:
"non-static variable this cannot be referenced from a static context"?
(7 answers)
Closed 10 years ago.
Receiving error non static variable this cannot be referenced from a static context when compiling the following code; what is the problem?
class bwCalc {
class Tst{
public void tst() {
byte[] data = new byte[1024];//1 kb buffer
int count;
long startedAt = System.currentTimeMillis();
while((System.currentTimeMillis()-startedAt)<1) {
System.out.println("hello\n");
}
}
}
public static void main(String argc[]) {
Tst c = new Tst();
c.tst();
}
}
You need the Outer class instance.
bwCalc b = newbwCalc ();
Tst c = b.new Tst();
c.tst();
Or simply make the inner class static.
The Tst class should be static, because it's not attached to a particular instance of bwCalc.

Categories