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
1 answer

How to open a (html) file as a stream from the project in Java?

This is what my project setup looks like: ' I'm trying to open the base.html file with: InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("/HttpServer/htmlFiles/base.html"); But it's always returning null. No…
mjuopperi
  • 773
  • 7
  • 25
0
votes
1 answer

How do I use RSpec to test that a class method called at class definition time gets executed?

How do I write a test to ensure that ::base_uri is part of the class definition, for MyClass? Sample code follows. require 'httparty' class MyClass include HTTParty base_uri 'https://www.google.com' end describe MyClass do it "should call…
nc.
  • 7,179
  • 5
  • 28
  • 38
0
votes
1 answer

Why are my class methods unbound?

Here's a snippet of a python class I am working on. (Sorry I can't go into too much detail. My organization gets a little antsy about such things. class my_class: @classmethod def make_time_string(cls, **kwargs): # does some stuff …
bob.sacamento
  • 6,283
  • 10
  • 56
  • 115
0
votes
2 answers

Python - Using Class Method

I have a class to represent NFL quarterbacks. In each instance will be a list of dictionaries, where each entry contains information regarding the QB stats for the game played. I need to generate the average number of yards gained per game based…
appleLover
  • 14,835
  • 9
  • 33
  • 50
0
votes
3 answers

Break method 1 from method 2 which is called in method 1 python

first I should say I don't know if the topic is good enough or not, but explaining my problem in one line is really hard. After that, I'm gonna explain you the whole event; I'm writing a program in python in which I have a class with some methods…
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
0
votes
1 answer

How to define class methods in JavaScript to insure modularity and performance

In Google developers article about JavaScript code optimization, they recommend in the section "Defining class methods" to use prototype to add methods as properties to a function (which is considered as an object in JavaScript). for instance,…
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
0
votes
2 answers

RUBY no method error for class method

writing a rational class and i'm getting an error although I can't see anything wrong with my code. here is the class&relevant method: class Rational attr_reader :numerator, :denominator def initialize(numerator=1,denominator=1) …
private_nodez
  • 41
  • 1
  • 5
0
votes
1 answer

Ruby: Method within a class refuses to be modified over multiple lines

I am currently doing an rspec ruby tutorial. One of the problems requires me to write a book_title program that utilizes some of the capitalization rules in English. The tests are extremely long but to give you a few ideas of what it is asking for…
0
votes
1 answer

Cannot pass arrays with content, using sharedStore?

Short background: I am creating an app in which the user can see the menus for each restaurant. I have created a class called "Dish" in which the name, ingredients and price of a menu item is set. I have then created a class for each restaurant…
OscarWyck
  • 2,515
  • 5
  • 21
  • 26
0
votes
1 answer

Ruby on Rails Class method scoped objects references

I was wondering If I could modify objects using a class method. For example, users = User.scoped # This will select all the objects And, suppose, I want to assign a variable for each of the object there is. Let's say, I want them to share a single…
Dmitri
  • 2,451
  • 6
  • 35
  • 55
0
votes
1 answer

I can't figure out how to call Java oscar3 class method stringToString from the command line

I'm trying to call a Java class method from the command line but the for the life of me can't figure out the syntax. The method is stringToString found in this class http://apidoc.ch.cam.ac.uk/oscar3/apidocs/uk/ac/cam/ch/wwmm/oscar3/Oscar3.html What…
0
votes
2 answers

Using ClassBasedViews I want to retrieve records from my database in Django based on the user's email, which is stored in a session

For the past 2hrs i've been trying to accomplish the following in Django 1.5: I want to retrieve records from my database based on the user's email, which is stored in a session. Here are my ideal algorithms: In my function based view: 1. Attempt…
0
votes
1 answer

Ruby classes that provide identically named class and instance methods

For flexibility, I've seen some APIs w/ classes that provide identically named class and instance methods. Are there any reasons as to why this might be a bad idea?
dougiebuckets
  • 2,383
  • 3
  • 27
  • 37
0
votes
1 answer

Can I use a custom class method on NSArray to return a constant?

I frequently need to refer to an array of strings (let's say team names) in multiple places. The contents of that array to not change. To achieve this, I defined my own class, MyConstants. In the header I am defining some unrelated constant strings…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
0
votes
1 answer

Performing Restkit configuration via a class method in another class

In an effort to clean up my application delegate I have moved Restkit configuration (ie, starting with [RKObjectManager managerWithBaseURL:[MYAppConfig wsBaseUrl]];) to another class MYAppConfig in a static method +(RKObjectManager…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126