Many years ago I remember a fellow programmer counselling this:
new Some::Class; # bad! (but why?)
Some::Class->new(); # good!
Sadly now I cannot remember the/his reason why. :( Both forms will work correctly even if the constructor does not actually exist in the Some::Class module but instead is inherited from a parent somewhere....
I am now designing an SNMP library. The problem is caused by a special function like this,
*** GetTable(string id)
This function may return Variable[,] which is a two dimensional array sometimes, but also Variable[,,] and arrays with more dimensions. So I believe it is not reasonable to return fixed array such as Variable[,], Variable[...
The Java Tutorials say: "it is not possible for two invocations of synchronized methods on the same object to interleave."
What does this mean for a static method? Since a static method has no associated object, will the synchronized keyword lock on the class, instead of the object?
...
which is better ???
public class Order
{
private double _price;
private double _quantity;
public double TotalCash
{
get
{
return _price * _quantity;
}
}
or
public class Order
{
private double _totalCash;
private double _price;
private double _quantity;
private void CalcCashTotal()
{
_tot...
Duplicate: http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java
I've read this post already.
What does the "static" keyword in a method do?
I remember being told that static != clingy...but that is pretty much all I know about this keyword.
...
Say I have a DLL assembly, containing an auto-generated class with two methods:
public class AutoGeneratedClass
{
public int DoSomething(bool forReal) { ??? }
public void DoSomethingElse(string whatever) { ??? }
}
The methods could be anything, really. The above is just an illustration.
What kind of code would I need to gener...
Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks frameworks fail complaining that Bar is not a virtual/overridable method:
public interface IFoo
{
string Bar();
}
public class Foo : IFoo
{
public string Bar()
{
return "Bar";
}
}
[TestMethod]
public void MoqTest()
{
var f...
I'm looking for a way to write a custom .net class that would allow for nested methods.
For example... say I have a class X with a function Y that returns a list. Then I have another function that returns a sorted list...
I would like to be able to do something like x.y().z() where z would accept the output of y() as its input.
Basic...
I have this showing up all over in my controllers:
if not session[:admin]
flash[:notice] = "You don't have the rights to do #{:action}."
redirect_to :action=>:index
return
end
And its sibling:
if not session[:user] and not session[:admin]
flash[:notice] = "You don't have the rights to do #{:action}."
redirect_to :action=>:i...
How do you call a client codes methods from inside a class defined in the client code?
For example, I have a memory reading class, that can read values from the memory of a process at a certain address. I also have classes for managing the type of data that is read from memory (I am reading about an in-game 'object'. In the 'client code...
Is it possible in Ruby to get a reference to methods of an object ( I would like to know if this can be done without procs/lambdas ) , for example , consider the following code :
class X
def initialize
@map = {}
setup_map
end
private
def setup_map
# @map["a"] = get reference to a method
# @map["b"] = get refere...
I saw this question asking about whether globals are bad.
As I thought about the ramifications of it, the only argument I could come up with that they're necessary in some cases might be for performance reasons.
But, I'm not really sure about that. So my question is, would using a global be faster than using a get/set method call?
G-...
Hello,
I have a questions about changing the values of variables in methods in Java.
I have the following example class:
public class Test {
public static void funk(int a, int[] b) {
b[0] = b[0] * 2;
a = b[0] + 5;
}
public static void main(String[] args) {
int bird = 10;
int[] tiger = {7};
T...
I'm using a Python API that expects me to pass it a function. However, for various reasons, I want to pass it a method, because I want the function to behave different depending on the instance it belongs to. If I pass it a method, the API will not call it with the correct 'self' argument, so I'm wondering how to turn a method into a fu...
#include "iostream"
#include "vector"
class ABC {
};
class VecTest {
std::vector<ABC> vec;
public:
std::vector<ABC> & getVec() const { //Here it errors out
return vec;
}
};
Removing the const fixes it , is it not the case that getVec is a constant method. So why is this not allowed?
...
I've created a method that generates a new class and adds some methods into the class, but there is a strange bug, and I'm not sure what's happening:
def make_image_form(image_fields):
''' Takes a list of image_fields to generate images '''
images = SortedDict()
for image_name in image_fields:
images[image_name] = fo...
I have a function that I use to add vectors, like this:
public static Vector AddVector(Vector v1, Vector v2)
{
return new Vector(
v1.X + v2.X,
v1.Y + v2.Y,
v1.Z + v2.Z);
}
Not very interesting. However, I overload the '+' operator for vectors and in the overload I call the AddVector function to avoid code duplica...
Is it like...
var obj = new Object();
obj.function1 = function(){
//code
}
or something like that?
...
can we change the name of the init() method of servlet?
i mean to say the life should be-
xyz()-service()-destroy()
instead of
init()-service()-destroy()
...
Exact duplicate by same poster: http://stackoverflow.com/questions/516291/the-use-of-this-in-java
Hello,
what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory?
The only situation I have encountered is when in the class you invoke a method within a method. But it is op...