CINXE.COM

Class Definition - What is a object-oriented programming class?

<!doctype html> <html lang="en"> <head> <title>Class Definition - What is a object-oriented programming class?</title> <link rel="canonical" href="https://techterms.com/definition/class"> <meta name="description" content="Learn how classes in object-oriented programming serve as templates for objects."> <meta name="keywords" content="Class, definition, Java, OOP, object-oriented programming, method, attributes, public, encapsulated"> <meta name="robots" content="max-image-preview:large"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="apple-itunes-app" content="app-id=923672016"> <meta charset="utf-8"> <link rel="preconnect" href="https://www.google-analytics.com"> <link rel="preconnect" href="securepubads.g.doubleclick.net"> <link rel="preconnect" href="https://www.googletagservices.com"> <link rel="stylesheet" href="https://techterms.com/css/tt.css?v=1" media="screen, print"> <link rel="icon" href="/svg/favicon.svg"> <script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('consent','default',{'ad_storage':'granted','ad_user_data':'granted','ad_personalization':'granted','analytics_storage':'granted'});gtag('consent','default',{'ad_storage':'denied','ad_user_data':'denied','ad_personalization':'denied','region':["NO","DE","FI","BE","PT","BG","DK","LT","LU","LV","HR","FR","HU","SE","SI","MC","SK","MF","SM","YT","IE","GF","EE","MQ","MT","GP","IS","GR","IT","ES","AT","RE","CY","CZ","AX","PL","RO","LI","NL"]})</script> <script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" data-domain-script="018decfc-da98-7112-b546-affa65c740cd"></script> <script>let adsLoaded=!1;let cmpReady=!1;let adsReady=!1;function OptanonWrapper(){cmpReady=!0;loadAds()}function loadAds(){if(!adsLoaded&&cmpReady&&adsReady){if((typeof __tcfapi!=="undefined")||(OnetrustActiveGroups.indexOf('C0004')!=-1)){googletag.cmd.push(function(){googletag.pubads().refresh()});!function(e,t,a,n,o,s){o=e.createElement(t),s=e.getElementsByTagName(t)[0],o.async=1,o.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",o.setAttribute("data-ad-client","ca-pub-0087137714961591"),s.parentNode.insertBefore(o,s)}(document,"script")}else{googletag.cmd.push(function(){googletag.pubads().setPrivacySettings({nonPersonalizedAds:!0}),googletag.pubads().refresh()});!function(e,t,a,n,o,s){o=e.createElement(t),s=e.getElementsByTagName(t)[0],o.async=1,o.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",o.setAttribute("data-ad-client","ca-pub-0087137714961591"),o.setAttribute("data-privacy-treatments","disablePersonalization"),s.parentNode.insertBefore(o,s)}(document,"script")} adsLoaded=!0}}</script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-5K7VMN5PX3"></script> <script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments)}gtag('js',new Date());gtag('config','G-5K7VMN5PX3')</script> <script>for(var ca=document.cookie.split("; "),i=0;i<ca.length;i++){var c=ca[i];if(-1!=c.indexOf("tt_theme=")){var theme=c.substring(9,c.length),html=document.getElementsByTagName("html")[0];html.classList.add("theme-"+theme),USERTHEME=theme;}else-1!=c.indexOf("tt_size=")&&(USERSIZE=c.substring(8,c.length))}</script> <script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script> <script>var sizes_tlb = [[970, 90], [728, 90], [468, 60], [320, 100], [320, 50]];var sizes_ss = [[300, 600], [160, 600], [468, 60], [320, 100], [320, 50]];var sizes_ra = [[336, 280], [300, 250], [320, 100], [320, 50]];var sizes_lb = [[728, 90], [468, 60], [320, 100], [320, 50], "fluid"];window.googletag = window.googletag || {cmd: []}; googletag.cmd.push(function() {function defineAdSlot(e,d,g,i){e="/1009901/"+e,g="div-gpt-ad-"+g+"-0",slot0=googletag.defineSlot(e,d,g).defineSizeMapping(i).addService(googletag.pubads())}var mapping_tlb = googletag.sizeMapping().addSize([1000, 0], [[970, 90], [728, 90], [468, 60], [320, 100], [320, 50]]).addSize([758, 0], [[728, 90], [468, 60], [320, 100], [320, 50]]).addSize([488, 0], [[468, 60], [320, 100], [320, 50]]).addSize([0, 0], [[320, 100], [320, 50]]).build();var mapping_ss = googletag.sizeMapping().addSize([981, 0], [[300, 600], [160, 600]]).build();var mapping_ra = googletag.sizeMapping().addSize([971, 0], [[336, 280], [300, 250], [320, 100], [320, 50]]).addSize([551, 0], [[300, 250]]).build();var mapping_lb = googletag.sizeMapping().addSize([1178, 0], [[728, 90], [468, 60], [320, 100], [320, 50], "fluid"]).addSize([966, 0], [[468, 60], [320, 100], [320, 50], "fluid"]).addSize([820, 0], [[728, 90], [468, 60], [320, 100], [320, 50], "fluid"]).addSize([520, 0], [[468, 60], [320, 100], [320, 50], "fluid"]).addSize([0, 0], [[320, 100], [320, 50], "fluid"]).build();var mapping_qlb = googletag.sizeMapping().addSize([758, 0], [[728, 90], [468, 60], [320, 100], [320, 50], "fluid"]).addSize([488, 0], [[468, 60], [320, 100], [320, 50], "fluid"]).addSize([0, 0], [[320, 100], [320, 50], "fluid"]).build();if (window.innerWidth >= 0) defineAdSlot('tt_tlb_2020', sizes_tlb, '1589388248615', mapping_tlb);if (window.innerWidth >= 551) defineAdSlot('tt_dra_2020', sizes_ra, '1589302365809', mapping_ra);if (window.innerWidth >= 0) defineAdSlot('tt_dlb_2020', sizes_lb, '1589236206757', mapping_lb);if (window.innerWidth >= 980) defineAdSlot('tt_dss_2020', sizes_ss, '1589233879778', mapping_ss);});googletag.cmd.push(function(){googletag.pubads().disableInitialLoad(),googletag.pubads().enableSingleRequest(),googletag.pubads().setTargeting("page_url","/definition/class"),googletag.enableServices()});adsReady=true;loadAds();if (window.innerWidth >= 0) googletag.cmd.push(function(){googletag.display("div-gpt-ad-1589388248615-0")});if (window.innerWidth >= 551) googletag.cmd.push(function(){googletag.display("div-gpt-ad-1589302365809-0")});if (window.innerWidth >= 0) googletag.cmd.push(function(){googletag.display("div-gpt-ad-1589236206757-0")});if (window.innerWidth >= 980) googletag.cmd.push(function(){googletag.display("div-gpt-ad-1589233879778-0")});</script> <script type="application/ld+json"> {"@context":"http://schema.org","@type":"Article","dateCreated":"2011-04-18","datePublished":"2011-04-18","dateModified":"2011-04-18","copyrightYear":"2011","copyrightHolder":{"@type":"Organization","name":"Sharpened Productions"},"name":"Class","description":"A technical definition of Class","headline":"Class","abstract":"Definition of Class","author":{"@type":"Person","name":"Per Christensson","url":"https://techterms.com/authors/per_christensson"},"publisher":{"@type":"Organization","name":"TechTerms.com","logo":"https://techterms.com/img/main/techterms.png"},"teaches":{"@type":"DefinedTerm","url":"https://techterms.com/definition/class","name":"Class","description":"A technical definition of Class"},"audience":{"@type":"Audience","audienceType":"everyone"},"genre":"Technology","inLanguage":"en-US","isFamilyFriendly":"True","keywords":"Java, OOP, object-oriented programming, method, attributes, public, encapsulated","locationCreated":{"@type":"Place","name":"United States"},"text":"A class is used in object-oriented programming to describe one or more objects. It serves as a template for creating, or instantiating, specific objects within a program. While each object is created from a single class, one class can be used to instantiate multiple objects.","url":"https://techterms.com/definition/class"} </script> </head> <body> <div id="full"> <header id="siteheader"> <div class="content"> <a href="#main" class="visuallyhidden">Skip to main content</a> <a href="/" class="logo"><img src="/svg/logo.svg" width="212" height="24" alt="TechTerms.com"></a> <div class="siteheader-middle"> <div class="searchwrapper collapsed"> <form id="search" class="search" action="/search" method="post"> <div class='autocomplete' role='combobox' aria-expanded='false' aria-owns='autocomplete-results'> <input type="text" id="term" name="term" placeholder="Search (e.g. class, ASP, PHP, C#)" aria-label="Search (e.g. psd, docx, exe, jpg, zip)" autocomplete="off" class='autocomplete-input term' aria-autocomplete='both' aria-controls='autocomplete-results'> <button id="searchsubmit" class="searchsubmit"></button> </div> <ul id='autocomplete-results' class='autocomplete-results hidden' aria-label='Search'> </ul> </form> </div> <a href="/random" class="headerbutton random" id="random"><img src="/svg/random.svg" alt="random" width="16" height="16"></a> <button id="searchtoggle"><img src="/svg/search-white.svg" alt="search" width="18" height="18"></button> </div> <div class="siteheader-right"> <button id="menuopen"><img src="/svg/menu.svg" alt="open menu" width="18" height="16"></button> <nav id="sitemenu" class="collapsed"> <button id="menuclose"><img src="/svg/close.svg" alt="close menu" width="14" height="14"></button> <ul> <li><a href="/browse/">Terms</a></li> <li><a href="/quiz/">Quizzes</a></li> <li><a href="/help/">Help Center</a></li> <li class="mobile"><a href="/random">Random Term</a></li> <li class="settings"><button id="settings"><img src="/svg/settings.svg" alt="settings" width="22" height="22"></button> <div class="settingsmenu"> <fieldset> <legend>Theme</legend> <input id="theme-default" value="default" type="radio" name="theme" checked> <label for="theme-default">Light</label> <input id="theme-dark" value="dark" type="radio" name="theme" > <label for="theme-dark">Dark</label> <input id="theme-hc" value="hc" type="radio" name="theme" > <label for="theme-hc">High Contrast</label> </fieldset> <div class="fontsizewrapper"><label for="fontsize">Font Size<input type="range" id="fontsize" name="fontsize" min="14" max="26" step="2" value="20"></label><span class="currentsize">20</span></div> <button id="resetsettings">Reset</button> </div> </li> </ul> </nav> </div> </div> </header> <main> <div class="adTopLB"> <div id='div-gpt-ad-1589388248615-0'> </div> </div> <div id="main" class="def"> <div id="left"> <div class="card hasheader"> <div class="cardheader"> <div class="breadcrumbs"><a href="/">Home</a><svg viewBox="0 0 9 14" xmlns="http://www.w3.org/2000/svg"><path d="m0 14 9-7-9-7z"/></svg><a href="/category/software">Software Terms</a><svg viewBox="0 0 9 14" xmlns="http://www.w3.org/2000/svg"><path d="m0 14 9-7-9-7z"/></svg>Class Definition</div> </div> <article> <h1>Class</h1> <div class="adDefRect"> <div id='div-gpt-ad-1589302365809-0'> </div> </div> <p>A class is used in <a href="/definition/oop">object-oriented programming</a> to describe one or more objects. It serves as a template for creating, or instantiating, specific objects within a <a href="/definition/program">program</a>. While each object is created from a single class, one class can be used to instantiate multiple objects.</p> <p>Several programming languages support classes, including <a href="/definition/java">Java</a>, <a href="/definition/cplusplus">C++</a>, Objective C, and <a href="/definition/php">PHP</a> 5 and later. While the <a href="/definition/syntax">syntax</a> of a class definition varies between programming languages, classes serve the same purpose in each language. All classes may contain variable definitions and <a href="/definition/method">methods</a>, or subroutines that can be run by the corresponding object.</p> <p>Below is an example of a basic Java class definition:</p> <p><span class="command">class Sample<br></span> <span class="command">{<br></span> <span class="command">&nbsp;&nbsp; public static void main(String[] args)<br></span> <span class="command">&nbsp;&nbsp; {<br></span> <span class="command">&nbsp;&nbsp; &nbsp;&nbsp; String sampleText = "Hello world!";<br></span> <span class="command">&nbsp;&nbsp; &nbsp;&nbsp; System.out.println(sampleText);<br></span> <span class="command">&nbsp;&nbsp; }<br></span> <span class="command">}</span></p> <p>The above class, named <span class="command">Sample</span>, includes a single method named <span class="command">main</span>. Within <span class="command">main</span>, the variable <span class="command">sampleText</span> is defined as "Hello world!" The <span class="command">main</span> method invokes the <span class="command">System</span> class from Java's built-in core library, which contains the <span class="command">out.println</span> method. This method is used to print the sample text to the text <a href="/definition/output">output</a> window.</p> <p>Classes are a fundamental part of object-oriented programming. They allow variables and methods to be isolated to specific objects instead of being accessible by all parts of the program. This encapsulation of data protects each class from changes in other parts of the program. By using classes, developers can create structured programs with <a href="/definition/source_code">source code</a> that can be easily modified.</p> <p><b>NOTE:</b> While classes are foundational in object-oriented programming, they serve as blueprints, rather than the building blocks of each program. This is because classes must be instantiated as objects in order to be used within a program. Constructors are typically used to create objects from classes, while destructors are used to free up resources used by objects that are no longer needed.</p> <div class="adBotLB"> <div id='div-gpt-ad-1589236206757-0'> </div> </div> <div class="cardfooter"> <div class="upDate"><div class="author">Updated <span class="pdate">April 18, 2011</span> by <a href="/authors/per_christensson">Per C.</a></div></div><div id="citations"> <div class="tabs"> <div role="tablist" aria-label="citations"> <button role="tab" aria-selected="false" aria-controls="apa" tabindex="-1">APA</button> <button role="tab" aria-selected="false" aria-controls="mla" tabindex="-1">MLA</button> <button role="tab" aria-selected="false" aria-controls="chicago" tabindex="-1">Chicago</button> <button role="tab" aria-selected="false" aria-controls="html" tabindex="-1">HTML</button> <button role="tab" aria-selected="false" aria-controls="link" tabindex="-1">Link</button> </div> <div class="citationwrap"> <div id="apa" role="tabpanel" class="citation" hidden> </div> <div id="mla" role="tabpanel" class="citation" hidden> </div> <div id="chicago" role="tabpanel" class="citation" hidden> </div> <div id="html" role="tabpanel" class="citation" hidden> </div> <div id="link" role="tabpanel" class="citation" hidden> <div>https://techterms.com/definition/class</div> </div> <button id="citecopy"><span class="visuallyhidden">Copy</span><svg enable-background="new 0 0 488.3 488.3" viewBox="-7.5 -7.5 495.8 503.3" xmlns="http://www.w3.org/2000/svg"><path d="m314.25 85.4h-227c-21.3 0-38.6 17.3-38.6 38.6v325.7c0 21.3 17.3 38.6 38.6 38.6h227c21.3 0 38.6-17.3 38.6-38.6v-325.7c-.1-21.3-17.4-38.6-38.6-38.6zm11.5 364.2c0 6.4-5.2 11.6-11.6 11.6h-227c-6.4 0-11.6-5.2-11.6-11.6v-325.6c0-6.4 5.2-11.6 11.6-11.6h227c6.4 0 11.6 5.2 11.6 11.6z" stroke-width="15"/><path d="m401.05 0h-227c-21.3 0-38.6 17.3-38.6 38.6 0 7.5 6 13.5 13.5 13.5s13.5-6 13.5-13.5c0-6.4 5.2-11.6 11.6-11.6h227c6.4 0 11.6 5.2 11.6 11.6v325.7c0 6.4-5.2 11.6-11.6 11.6-7.5 0-13.5 6-13.5 13.5s6 13.5 13.5 13.5c21.3 0 38.6-17.3 38.6-38.6v-325.7c0-21.3-17.3-38.6-38.6-38.6z" stroke-width="15"/></svg></button> </div> </div> </div> </div> </article> </div> <div id="defquiz" class="card"> <h2 class="heading logoheading"><img src="/img/main/quiz.png" alt="quiz" style="width:45px">Test Your Knowledge</h2> <div class="quizBox"> <div class="quizBody"> <p class="quizQuestion">Which of the following is not a type of Unix shell?</p> <div class="quizTable"> <div id="ansButtonA" class="answerButton activeButton" data-answer="Anum" data-date="2023-04-05"><span class="ansLetter">A</span> <div class="answerCell"><span id="answerA" class="quizAnswer">Bourne</span> <div class="answerBar"> <div id="quizBarA" class="quizBar"></div><span id="quizPercA" class="quizperc">0%</span> </div> </div> </div> <div id="ansButtonB" class="answerButton activeButton" data-answer="Bnum" data-date="2023-04-05"><span class="ansLetter">B</span> <div class="answerCell"><span id="answerB" class="quizAnswer">Z shell</span> <div class="answerBar"> <div id="quizBarB" class="quizBar"></div><span id="quizPercB" class="quizperc">0%</span> </div> </div> </div> <div id="ansButtonC" class="answerButton activeButton" data-answer="Cnum" data-date="2023-04-05"><span class="ansLetter">C</span> <div class="answerCell"><span id="answerC" class="quizAnswer">PowerShell</span> <div class="answerBar"> <div id="quizBarC" class="quizBar"></div><span id="quizPercC" class="quizperc">0%</span> </div> </div> </div> <div id="ansButtonD" class="answerButton activeButton" data-answer="Dnum" data-date="2023-04-05"><span class="ansLetter">D</span> <div class="answerCell"><span id="answerD" class="quizAnswer">Bash</span> <div class="answerBar"> <div id="quizBarD" class="quizBar"></div><span id="quizPercD" class="quizperc">0%</span> </div> </div> </div> <div class="quizResult"><div class="qmsg"> <b class="quizPass">Correct!</b> <b class="quizFail">Incorrect!</b> &nbsp; <span class="customMessage"></span>&nbsp; View the <a href="/definition/shell">Shell definition</a>.</div> <a href="/quiz/#morequizzes" class="quizNext btnprimary">More Quizzes →</a> </div> </div> </div> </div> </div> </div> <div id="right"> <div class="prevnext"> <a href="cisc"><span class="arrow">‹</span> &nbsp;<span class="linktext">CISC</span></a><span>&nbsp;|&nbsp;</span><a href="clean_install"><span class="linktext">Clean Install</span>&nbsp; <span class="arrow">›</span></a></div> <div class="tf card"> <h2 class="heading">Tech Factor</h2> <a href="/help/tech_factor" class="info" title="What is the Tech Factor?">?</a> <a href="/techfactor/8"><span class="factor">8</span><span class="denom"> / 10</span></a></div> <div class="adRightSky"> <div id='div-gpt-ad-1589233879778-0'> </div> </div> </div> <div class="techterms card"> <h2 class="heading">The Tech Terms Computer Dictionary</h2> <p>The definition of Class on this page is an original definition written by the <a href="/authors/">TechTerms.com team</a>. If you would like to reference this page or cite this definition, please use the green citation links above.</p> <p>The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please <a href="/contact">contact us</a>.</p> </div> </div> <div class="popupwrapper"> <div id="newsletter_popup"> <button class="close">×</button> <div id="ttnform"> <h3 class="heading">Sign up for the free TechTerms&nbsp;Newsletter</h3> <form id="subscribe"> <div class="field"><label for="newsletter_email">Email (required):</label> <input type="email" id="newsletter_email" name="newsletter_email" required aria-label="email address"> </div> <div class="row1"> <div class="field"><label for="first_name">First Name:</label> <input type="text" name="first_name" id="first_name"></div> <div class="field"><label for="first_name">Last Name:</label> <input type="text" name="last_name" id="last_name"></div> </div> <fieldset class="row2"> <legend>How often would you like to receive an email?</legend> <input type="radio" name="frequency" id="daily" value="d" checked><label for="daily">Daily</label> <input type="radio" name="frequency" id="weekly" value="w"><label for="weekly">Weekly (Every Tuesday)</label> <input type="text" id="newsletter_message" name="message" class="cfieldhid" aria-hidden="true" aria-label="If you are a human, please do not fill in this field." tabindex="-1"> </fieldset> <input type="submit" class="btnprimary" value="Subscribe"> <div id="ttnerror"> </div> <p>You can unsubscribe or change your frequency setting at any time using the links available in each email.<br><br>Questions? Please <a href="/contact">contact&nbsp;us</a>.</p> </form> </div> <div id="ttnconfirm" style="display: none;"> <h3>Thank You</h3> <p>We just sent you an email to confirm your email address. Once you confirm your address, you will begin to receive the newsletter.</p> <p>If you have any questions, please <a href="/contact">contact us</a>.</p> <button class="ok btnprimary">Ok</button> </div> </div> </div> </main> <footer id="sitefooter"><a href="https://sharpened.com/" target="_blank" rel="noopener"><span class="copyright">&copy; 2024 Sharpened Productions</span></a> &nbsp;|&nbsp; <a href="/terms">Terms of Use</a> &nbsp;|&nbsp; <a href="/privacy">Privacy Policy</a> &nbsp;|&nbsp; <button id="ot-sdk-btsn" class="ot-sdk-show-settings">Cookie Settings</button> &nbsp;|&nbsp; <a href="/about">About</a> &nbsp;|&nbsp; <a href="/contact">Contact</a></footer> </div> <script defer src="https://techterms.com/js/tt.js"></script><script> var cterm = 'Class'; var curl = 'class'; quizDate = '2023-04-05'; T = cterm; C = "b2bc56ff17dbf03758d9b18a2c9d1cf5"; </script> <script defer src="https://techterms.com/js/quiz.js?v=1"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10