Questions tagged [non-static]

non-static is a term to define a function or field that is bound to some object instance. Without an instance, non static fields cannot be accessed and non static methods cannot be invoked. Unlike static, non-static methods can be overridden (virtual).

525 questions
361
votes
15 answers

Non-static variable cannot be referenced from a static context

I've written this test code: class MyProgram { int count = 0; public static void main(String[] args) { System.out.println(count); } } But it gives the following error: Main.java:6: error: non-static variable count cannot be…
Greg
  • 3,643
  • 3
  • 16
  • 3
143
votes
14 answers

Calling Non-Static Method In Static Method In Java

I'm getting an error when I try to call a non-static method in a static class. Cannot make a static reference to the non-static method methodName() from the type playback I can't make the method static as this gives me an error too. This static…
me123
  • 1,483
  • 3
  • 14
  • 11
104
votes
10 answers

Difference between Static methods and Instance methods

I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what…
Panthy
  • 1,605
  • 4
  • 19
  • 27
72
votes
4 answers

Non-static method ..... should not be called statically

I have recently done an update to PHP 5.4, and I get an error about static and non-static code. This is the error: PHP Strict Standards: Non-static method VTimer::get() should not be called statically in…
47
votes
3 answers

C# error: "An object reference is required for the non-static field, method, or property"

I have two classes, one for defining the algorithm parameters and another to implement the algorithm: Class 1 (algorithm parameters): using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace VM_Placement { …
user1277070
  • 765
  • 3
  • 10
  • 15
44
votes
5 answers

C++ Overload Static Function with Non-Static Function

I would like to print two different things depending on whether a function is called statically with Foo::print() or from an instance of Foo foo; foo.print(); EDIT: Here is a class definition that definitely does not work, as answered by a few…
Alan Turing
  • 12,223
  • 16
  • 74
  • 116
40
votes
4 answers

What is the difference between static methods in a Non static class and static methods in a static class?

I have two classes Class A and ClassB: static class ClassA { static string SomeMethod() { return "I am a Static Method"; } } class ClassB { static string SomeMethod() { return "I am a Static Method"; } } I…
Vamsi
  • 4,237
  • 7
  • 49
  • 74
37
votes
3 answers

invalid use of non-static member function

I have something like this: class Bar { public: pair one; std::vector cars; Bar(string one, string two, string car); }; class Car { public: string rz; …
Nash
  • 493
  • 1
  • 4
  • 7
30
votes
7 answers

Does it make sense to have a non static method which does not use an instance variable?

The compiler does not let a static method call a non static method. I understand it does this because a not static method usually ends up using an instance variable. But does it make sense to have a non static method which does not use an instance…
Chiseled
  • 2,280
  • 8
  • 33
  • 59
26
votes
4 answers

What does "operator = must be a non-static member" mean?

I'm in the process of creating a double-linked list, and have overloaded the operator= to make on list equal another: template void operator=(const list& lst) { clear(); copy(lst); return; } but I get this error when I try…
user98188
19
votes
6 answers

What Cases Require Synchronized Method Access in Java?

In what cases is it necessary to synchronize access to instance members? I understand that access to static members of a class always needs to be synchronized- because they are shared across all object instances of the class. My question is when…
user39732
  • 195
  • 1
  • 3
  • 7
17
votes
4 answers

Ambiguous call from static context in Java

The main method tries to access var, but results in ambiguous call. Why? Instance variable var in Base1 isn't accessible (visible?) from static context anyway. class Base1 { int var; } interface Base2 { public static final int var…
user3613844
  • 273
  • 2
  • 8
16
votes
8 answers

std::sort function with custom compare function results error: reference to non-static member function must be called

I have trouble using the std::sort function with my custom comparison function when defined inside a class. class Test { private: vector< vector > mat; bool compare(vector, vector); public: void…
Lennart
  • 161
  • 1
  • 1
  • 3
16
votes
2 answers

Non static members as default parameters in C++

I'm refactoring a large amount of code where I have to add an extra parameter to a number of functions, which will always have a value of a member of that object. Something like class MyClass { public: CMyObject A,B; void MyFunc(CMyObject…
SmacL
  • 22,555
  • 12
  • 95
  • 149
16
votes
8 answers

Why do I get "non-static variable this cannot be referenced from a static context"?

I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent's class I get : non-static variable this cannot be referenced from a static context On the other hand when I put the sublass…
Patryk
  • 22,602
  • 44
  • 128
  • 244
1
2 3
34 35