Meta tags:
Headings (most frequently used words):
static, javascript, the, for, web, fields, methods, objects, initialization, blocks, summary, chapter, article, toc, about, getting, started, basics, control, structures, data, types, built, in, operators, functions, classes, error, handling, regular, expressions, general, bom, dom,
Text of the page (most frequently used words):
the (94), dog (71), static (67), this (40), name (38), class (33), age (25), and (24), #fields (23), you (22), for (19), let (18), #methods (17), random (16), math (15), createdog (15), can (14), have (14), javascript (13), initialization (13), are (13), dognames (13), introduction (12), instance (12), alert (12), method (12), with (11), field (11), describe (11), just (10), like (10), but (10), block (10), how (10), members (9), new (9), dogcounter (9), return (9), list (8), used (8), example (8), that (8), names (8), from (8), see (8), not (7), use (7), function (7), our (7), floor (7), they (6), while (6), now (6), result (6), will (6), one (6), then (6), operators (6), regular (5), values (5), when (5), data (5), called (5), which (5), object (5), many (5), dogs (5), length (5), constructor (5), blocks (5), pluto (5), would (5), create (5), functions (5), operator (5), tutorial (4), article (4), inheritance (4), declared (4), show (4), useful (4), created (4), simply (4), generatedogname (4), completely (4), randomly (4), also (4), dog1 (4), dog2 (4), dog3 (4), try (4), doe (4), been (3), into (3), your (3), language (3), previous (3), them (3), well (3), assign (3), more (3), complex (3), instances (3), all (3), add (3), array (3), track (3), const (3), chars (3), counter (3), years (3), old (3), possible (3), bit (3), need (3), notice (3), using (3), keyword (3), catch (3), error (3), john (3), last (3), first (3), sense (3), web (3), expressions (3), catching (3), getters (3), setters (3), here (2), alternative (2), sharing (2), same (2), between (2), utility (2), summary (2), quite (2), silly (2), several (2), could (2), very (2), cases (2), where (2), uses (2), generated (2), keeping (2), initializing (2), charslength (2), dognamelength (2), total (2), added (2), inside (2), string (2), works (2), action (2), look (2), hopefully (2), these (2), start (2), however (2), most (2), actual (2), what (2), than (2), logic (2), including (2), handling (2), multiple (2), define (2), increment (2), always (2), access (2), period (2), parameter (2), less (2), variables (2), assigns (2), prefixed (2), full (2), specific (2), randomizeddog (2), complete (2), number (2), world (2), makes (2), classes (2), asp (2), net (2), dom (2), window (2), bom (2), search (2), types (2), conditional (2), errors (2), prototypes (2), objects (2), parameters (2), basics (2), loops (2), statement (2), toc (2), localization, suggest, correction, contact, com, 2007, 2026, has, fully, translated, following, languages, preferred, help, translate, click, czech, next, assigned, declaring, may, form, unlike, referenced, directly, insted, generally, functionality, relating, again, point, does, techniques, prove, various, stuff, happens, produces, set, characters, lower, part, call, loop, final, modification, generates, stores, instantiate, selected, abcdefghijklmnopqrstuvwxyz, charat, touppercase, push, further, illustrate, flexible, generate, rewritten, admit, starting, overly, bear, proves, powerful, features, processed, end, likely, before, allowing, rely, soon, followed, code, wanted, something, assigning, there, reasons, thanks, any, kind, etc, subject, concept, basically, constructors, value, allows, keep, line, referencing, requires, input, returns, case, two, latter, turn, picks, along, below, remember, declare, shared, piece, single, cached, expanding, above, couple, defined, lines, still, normal, get, calling, whether, within, outside, specify, obviously, marking, passed, articles, might, terribly, real, give, idea, technique, adding, commonly, probably, often, related, enough, exist, factory, creating, usually, based, some, make, its, own, sometimes, bound, perfect, consider, storing, typically, want, belong, far, means, member, wpf, php5, jquery, html5, css3, webforms, mvc, location, history, script, tag, general, replace, capturing, groups, match, regex, flags, options, throwing, finally, extension, property, accessors, properties, recursive, callback, anonymous, default, optional, chaining, nullish, coalescing, ternary, remainder, logical, modify, decrement, arithmetic, comparison, assignment, json, date, built, type, conversion, null, undefined, strings, numbers, booleans, switch, control, structures, truthy, falsy, basic, syntax, hello, tools, getting, started, libraries, webbrowser, about, chapter, table, contents,
Text of the page (random words):
static fields methods the complete javascript tutorial javascript tutorial toc table of contents chapter article toc introduction fields methods getters setters inheritance static fields methods static methods static fields static initialization blocks summary about javascript what is javascript javascript and the webbrowser javascript libraries getting started the tools you need hello world the basics basic javascript syntax variables truthy falsy control structures the if statement the switch statement loops for and while loops for of and for in data types introduction booleans numbers strings null undefined type conversion built in objects introduction number string array the basics array methods date math json operators introduction the assignment operator comparison operators arithmetic operators increment decrement operators modify and assign operators logical operators remainder operator conditional ternary operator nullish coalescing operator optional chaining operator functions introduction function parameters function parameters with default values anonymous functions callback functions recursive functions multiple return values objects introduction properties methods property accessors getters setters prototypes prototypes inheritance extension classes introduction fields methods getters setters inheritance static fields methods error handling introduction catching errors try catch catching throwing errors try catch finally error types conditional catching regular expressions introduction regex flags options search match with regular expressions using capturing groups search replace with regular expressions javascript for web general introduction window dom bom the script tag javascript for web the bom the window object the history object the location object javascript for web the dom introduction javascript asp net mvc asp net webforms c css3 html5 jquery php5 wpf classes static fields methods so far all the class members fields and methods we have used in this tutorial are so called instance members this means that while they are declared on the class you need an instance of the class an object to access this member this makes perfect sense just consider our previous dog class with the name field for storing the name of the dog typically you would want to create more than one instance of a dog so the name would have to belong to the instance of the class and not the actual class however sometimes it makes sense to define members on a class which are not bound to an instance of the class members that are not instance members are called static members static methods most commonly used are probably static methods often used for utility functions not specific to an instance of the class but related enough to exist on the class an example of this is the so called factory methods used for creating instances of the class usually based on some logic which would make sense to have in its own function to show you an example of a static method in action i will add a method to our dog class from the previous articles to create a new dog with a random age this might not be terribly useful in the real world but hopefully it will give you an idea of how and when to use this technique first here s the method i will be adding to the class static createdog name let dog new dog dog name name dog age math floor math random 20 1 return dog notice that the name of the method is prefixed with the static keyword obviously marking this method as a static method inside of it we create a new instance of a dog assigns the passed in name and then we create a random number for the age field when calling a static method whether from within the class or outside of it you always specify the full class name first then a period and then the name of the method it would look like this dog createdog pluto now let s see the complete example including the static method class dog name age constructor name age this name name this age age static createdog name let dog new dog dog name name dog age math floor math random 20 1 return dog describe return this name is this age years old let dog new dog dog doe 7 alert dog describe let randomizeddog dog createdog pluto alert randomizeddog describe as you can see from the last lines of the example we can still create a dog like normal but as an alternative we now have the createdog method to get a dog with a random age static fields we also have static fields which are useful for sharing the same piece of data not specific to a single instance e g cached data and or data to be used by static methods let s try expanding the example above to use a couple of static fields but first let s see how a static field is defined class dog name age static dognames dog doe john dog pluto static dogcounter 0 as you can see the static fields are just prefixed with the static keyword just like static methods and they can be declared along regular fields just remember that when you declare a static field this field is now shared between all instances of the class as you ll see from the full example below this can be very useful in this case i have declared two static variables one for keeping track of how many dogs we have created and one is a list of possible dog names the latter will be used to turn the createdog into a completely parameter less function which randomly picks the name from the list of dog names and then assigns a random age as well let s see how it works class dog name age static dognames dog doe john dog pluto static dogcounter 0 constructor name age this name name this age age dog dogcounter static createdog let dog new dog dog name dog dognames math floor math random dog dognames length dog age math floor math random 20 1 return dog describe return this name is this age years old let dog1 dog createdog alert dog1 describe let dog2 dog createdog alert dog2 describe let dog3 dog createdog alert dog3 describe alert total dogs dog dogcounter as you can see the createdog method is now completely parameter less it requires no input but simply returns a dog with a name from our list as well as a random age you will also notice that we use the static dogcounter field in the constructor where we increment it by one this allows us to always keep track of how many dogs have been created we use this field in the last line of the example and as you can see just like a static method we access it by referencing the class name then a period and then the name of the field static initialization blocks as the last subject for this article i would like to show you the concept of static initialization blocks they are basically like constructors but for static fields so for instance we can define a static field and assign a value to it like this class dog static dognames dog doe john dog pluto but what if we wanted to do something a bit more complex than just assigning one or several static values to the field there could be many reasons for this and thanks to the static initialization block we can add any kind of logic to the initialization of our static fields including try catch blocks for error handling initializing multiple fields etc a static initialization block is added to a class simply by using the static keyword followed by a code block like this class dog static static initialization block you can have as many of these blocks as you d like they are simply processed from start to end however in most cases you ll likely just need the one also notice that the static initialization block is called before the actual constructor allowing you to rely on your static fields as soon as you start using the class to show you how the static initialization block works in action i have rewritten our dog class quite a bit it now uses a list of completely randomly generated names as the list of possible dog names i admit that this class is starting to look a bit silly and overly complex but just bear with me it hopefully proves how powerful these features are and how they can be used and just to further illustrate how flexible the javascript language is i have added a function inside the static initialization block to generate the random string we will use as a dog name class dog name age static dognames static dogcounter static alert initializing static fields function generatedogname let result const chars abcdefghijklmnopqrstuvwxyz const charslength chars length const dognamelength 8 let counter 0 while counter dognamelength result chars charat math floor math random charslength if result length 1 result result touppercase counter 1 return result this dognames while this dognames length 5 this dognames push generatedogname this dogcounter 0 constructor name age this name name this age age dog dogcounter static createdog let dog new dog dog name dog dognames math floor math random dog dognames length dog age math floor math random 20 1 return dog describe return this name is this age years old let dog1 dog createdog alert dog1 describe let dog2 dog createdog alert dog2 describe let dog3 dog createdog alert dog3 describe alert possible dog names dog dognames alert total dogs dog dogcounter with this final modification our dog class now generates a list of completely random dog names and stores them as a static field when you instantiate a new dog object it uses a randomly selected name from the list of randomly generated names while also keeping track of how many dogs have been created all the new stuff happens in the static initialization block where we have created a function called generatedogname it simply produces a set of 8 random characters which we ll use as a name in the lower part of the static initialization block i call the generatedogname in a while loop to add 5 random names to our dognames array again at this point this is quite a silly example but it does show several techniques that could prove very useful in various cases summary a class can have static members in the form of static fields and static methods unlike instance members static members are referenced directly on the class insted of on an instance of the class they are generally used for sharing the same data between instances of class static fields and for utility functionality relating to the class static methods static fields can be declared just like regular fields and values can be assigned to them when declaring them as well but as an alternative you may use a static initialization block to assign more complex values to the static fields inheritance previous introduction next this article has been fully translated into the following languages czech is your preferred language not on the list click here to help us translate this article into your language javascript tutorial com 2007 2026 contact us suggest correction localization
|