Questions tagged [class-method]

Methods that are called on a class instead of on an object.

Class methods are methods that are called on a class (compare this to class instance methods, or object methods). Its meaning may vary depending on the programming language: In some languages (e.g. C++, Java), class methods are synonymous with static methods (see section below), which are called with a known class name at compile-time. this cannot be used in static methods.

In some other languages (e.g. Smalltalk, Ruby, Objective-C), class methods are methods that are called on a class object, which can be computed at runtime, there being no difference between calling a method on a regular object or a class object; thus both instance and class methods are resolved dynamically, and there are no "static" methods. Notably, in these class methods, this refers to the class object.

Some languages have both. For example, in Python, one can create class methods and static methods using the classmethod and staticmethod decorators, respectively. The former has access to this (i.e. the instance object, conventionally known as self), while the latter does not.

904 questions
0
votes
2 answers

Objective-C class with multiple class variables

I am writing a class that has many class variables. So I am declaring the variables as static in my .m file and before @implementation statement, with setters and getters for them as class methods. Is it a good idea to do this for lets say more than…
0
votes
1 answer

can't call class method with parameter

I'm having trouble calling a class method from a View Controller. The class method takes a BOOL as input. Xcode allows me to call it without the parameter (which obviously crashes) but complains when I pass the parameter. Here is the relevant…
Paul Hoffer
  • 12,606
  • 6
  • 28
  • 37
0
votes
0 answers

What is the difference between accessing class methods via "." or "::"?

Given a Ruby class with a static method like class Foo def self.bar puts "Hello" end end I always accessed the method via Foo.bar However, I just noticed that you can also do Foo::bar Why is that? Is there a semantic difference? My…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0
votes
1 answer

Setting Property Variables in Class Methods

Currently, I understand that I cannot set Property Variables within a class method. For Example: #ISUser.h @interface ISUser : NSObject @property (nonatomic, retain) NSString *username; @property (nonatomic, retain) NSString *password; @property…
jsetting32
  • 1,632
  • 2
  • 20
  • 45
0
votes
4 answers

C++ function method via function pointer

I'm trying to understand how function pointer works can't clarified why I get the following err. #include using namespace std; enum COLOR {RED,BLACK,WHITE}; class Car {public: Car (COLOR c): color(c) { cout<<"Car's…
0
votes
1 answer

Instantiating instances of subclass from the parent class in Ruby

If I have a class B that inherits from class A, can I instantiate class B through a class method defined in class A? class A def self.instantiate params # ??? end end class B < A end b = B.instantiate 123 b.class # => B Obviously, I…
garbagecollector
  • 3,731
  • 5
  • 32
  • 44
0
votes
3 answers

Why can't I use the classname instead of self in objective c

It's just something which is not logical for me. Sure it's useful to call methods within in a class by the self-keyword. But why isn't it possible calling it by the own classname?? e.g. [MyClassWhereIAmActuallyIn anyRandomMethod]; instead of [self…
Michael
  • 1,030
  • 14
  • 29
0
votes
2 answers

Getting access to a sharedInstance from within a Class method

I am converting a project to an SDK. I need to convert several instance methods to class methods. I am getting a compiler warning about using "self". The warning is "Incompatible pointer types initializing Store* with an expression of Class. This…
jdog
  • 10,351
  • 29
  • 90
  • 165
0
votes
2 answers

C++: String member alias in method

My constructor: bnf::bnf(string encoded) { this->encoded = encoded; } copies the string data to a member. (Or does it..?) I will have a recursive decode method, but would like to avoid writing this->encoded all the time. How can I validly…
user76568
  • 456
  • 4
  • 16
0
votes
2 answers

Zendramework 2 ClassMethod Hydrator

I have underscore fields in my table like display_name I use ClassMethod hydrator to convert underscore to camel case but It doesn't work , (email property works but displayName doesn't work) this is my code : class UserEntity { protected…
Dante
  • 101
  • 12
0
votes
1 answer

How can I call a classmethod from another class in python?

I'm programming an app for the edXproyect using Django and I need to retrieve some information from auth_user table and I don't know how to do it. I have this: models.py from django.db import models from django.contrib.auth.models import…
0
votes
3 answers

How to reference a class method in a class attribute?

In a package, I have isolated a set of functions/variables that work altogether in a class. As these functions did not require a specific context, they became class methods. They may use some external functions, here represented by the save_c…
Guillaume Lemaître
  • 1,230
  • 13
  • 18
0
votes
4 answers

Python - classmethod( )

Reading Python Standard Library. Trying to understanding classmethod. class C: @classmethod def f(x,y): print('words') When I type: print(classmethod(C)) It returns: How would see what the…
Phoenix
  • 4,386
  • 10
  • 40
  • 55
0
votes
1 answer

Passing database querying tests using spec?

I am learning rspec and databases and have one movie database with the following schema. I'd like to know how to get this test to pass since my efforts seem to be futile. If you need any additional info please let me know in case I'm leaving…
John
  • 443
  • 8
  • 19
0
votes
1 answer

How to pass objects to the class method

I have the following class method that uses default classes if no provided: def self.make_sandwich(bread = Bread, butter = Butter, cheese = Cheese) ... end So I can call it and pass a new class of bread called…
meso_2600
  • 1,940
  • 5
  • 25
  • 50